arrow-functions

What's the meaning of “=>” (an arrow formed from equals & greater than) in JavaScript?

怎甘沉沦 提交于 2019-11-25 22:12:47
问题 I know that the >= operator means more than or equal to, but I\'ve seen => in some source code. What\'s the meaning of that operator? Here\'s the code: promiseTargetFile(fpParams, aSkipPrompt, relatedURI).then(aDialogAccepted => { if (!aDialogAccepted) return; saveAsType = fpParams.saveAsType; file = fpParams.file; continueSave(); }).then(null, Components.utils.reportError); 回答1: What It Is This is an arrow function. Arrow functions are a short syntax, introduced by ECMAscript 6, that can be

Are 'Arrow Functions' and 'Functions' equivalent / exchangeable?

ぐ巨炮叔叔 提交于 2019-11-25 22:09:57
问题 Arrow functions in ES2015 provide a more concise syntax. Can I replace all my function declarations / expressions with arrow functions now? What do I have to look out for? Examples: Constructor function function User(name) { this.name = name; } // vs const User = name => { this.name = name; }; Prototype methods User.prototype.getName = function() { return this.name; }; // vs User.prototype.getName = () => this.name; Object (literal) methods const obj = { getName: function() { // ... } }; //

What are the differences (if any) between ES6 arrow functions and functions bound with Function.prototype.bind?

南笙酒味 提交于 2019-11-25 22:03:19
It seems to me that, in ES6, the following two functions are very nearly identical: function () { return this; }.bind(this); () => { return this; }; The end result seems the same: arrow functions produce a JavaScript function object with their this context bound to the same value as the this where they are created. Obviously, in the general sense, Function.prototype.bind is more flexible than arrow functions: it can bind to values other than the local this , and it can bind any function's this at any point in time, potentially long after it is initially created. However, I'm not asking how

Using jQuery $(this) with ES6 Arrow Functions (lexical this binding)

谁说胖子不能爱 提交于 2019-11-25 22:02:54
问题 Using ES6 arrow functions with lexical this binding is great. However, I ran into an issue a moment ago using it with a typical jQuery click binding: class Game { foo() { self = this; this._pads.on(\'click\', function() { if (self.go) { $(this).addClass(\'active\'); } }); } } Using an arrow function instead: class Game { foo() { this._pads.on(\'click\', () => { if (this.go) { $(this).addClass(\'active\'); } }); } } And then $(this) gets converted to ES5 (self = this) type closure. Is a way to

What does “this” refer to in arrow functions in ES6?

霸气de小男生 提交于 2019-11-25 21:45:25
问题 I\'ve read in several places that the key difference is that \" this is lexically bound in arrow functions.\" That\'s all well and good, but I don\'t actually know what that means. I know it means it\'s unique within the confines of the braces defining the function\'s body, but I couldn\'t actually tell you the output of the following code, because I have no idea what this is referring to, unless it\'s referring to the fat arrow function itself....which doesn\'t seem useful. var testFunction

ECMAScript 6 arrow function that returns an object

耗尽温柔 提交于 2019-11-25 21:43:39
问题 When returning an object from an arrow function, it seems that it is necessary to use an extra set of {} and a return keyword because of an ambiguity in the grammar. That means I can’t write p => {foo: \"bar\"} , but have to write p => { return {foo: \"bar\"}; } . If the arrow function returns anything other than an object, the {} and return are unnecessary, e.g.: p => \"foo\" . p => {foo: \"bar\"} returns undefined . A modified p => {\"foo\": \"bar\"} throws “ SyntaxError : unexpected token:

When should I use Arrow functions in ECMAScript 6?

独自空忆成欢 提交于 2019-11-25 21:41:37
问题 The question is directed at people who have thought about code style in the context of the upcoming ECMAScript 6 (Harmony) and who have already worked with the language. With () => {} and function () {} we are getting two very similar ways to write functions in ES6. In other languages lambda functions often distinguish themselves by being anonymous, but in ECMAScript any function can be anonymous. Each of the two types have unique usage domains (namely when this needs to either be bound

Official information on `arguments` in ES6 Arrow functions?

白昼怎懂夜的黑 提交于 2019-11-25 19:46:04
(() => console.log(arguments))(1,2,3); // Chrome, FF, Node give "1,2,3" // Babel gives "arguments is not defined" from parent scope According to Babel (and from what I can tell initial TC39 recommendations), that is "invalid" as arrow functions should be using their parent scope for arguments. The only info I've been able to find that contradicts this is a single comment saying this was rejected by TC39, but I can't find anything to back this up. Just looking for official docs here. Bergi Chrome, FF, and node seem to be wrong here, Babel is correct: Arrow functions do not have an own arguments