arrow-functions

How to use arrow function with || operator

天涯浪子 提交于 2019-11-26 17:14:20
问题 Using Babel, I can see that callback = () => {}; compiles to callback = function callback() {}; which is what I expect. However I get an error when I try to use it with || callback = callback || () => {} Which I'd expect to be equivalent to callback = callback || function(){}; Why is this an error? Also, is there a more correct ES6 version of this familiar syntax? 回答1: It fails because that is just not valid syntax. Use the following to make it work: callback = callback || (() => {}) If you

When building classes in coffeescript, is there ever a reason to not use the fat arrow for the instance methods?

戏子无情 提交于 2019-11-26 16:30:09
问题 When building classes in coffeescript, is there ever a reason to not use the fat arrow for instance methods? Edit: Ok then! Great reply! :) To sum up, the problems are: - Takes more memory - Inability to patch - Begs question, why is it used for this method? Convention: - Be explicit when binding functions. - Declare fat arrowed methods in the constructor. - Use as much as you want, just not in class declarations. 回答1: Yes, there are reasons to not use the fat arrows always. In fact i'd argue

How can I differentiate between an arrow function, class and a normal function?

家住魔仙堡 提交于 2019-11-26 14:54:17
问题 How can I differentiate between these three things in ES6 using its reference? let x = i => i+1; class y { constructor(i) { this._i=i+1; } get i(){ return this._i;} } function z(i) { return i+1; } Example: test(x) //=> 'arrow' test(y) //=> 'class' test(z) //=> 'function' And how can I differentiate between these things in transpilers - Traceur / Babel? 回答1: How can I differentiate between these things in ES6? arrow functions are functions that cannot be used as constructors, and don't have a

Why does the logical or operator (||) with an empty arrow function (()=>{}) cause a SyntaxError?

懵懂的女人 提交于 2019-11-26 14:53:28
问题 In my code, I have something that boils down to this: var x = y || ()=>{}; (In case you are wondering, I am later calling x() and y may be defined as a function or it might not, so I don't want a TypeError to be thrown if it is not.) For some reason, this causes a SyntaxError: Unexpected token ) Why? I found out that var x = y || (()=>{}); works just fine, but y || ()=>{} dosen't work. Is this specced, or a bug in V8 or Chrome? (I tested this only in the latest release of Chrome stable.) 回答1:

When does the “fat arrow” (=>) bind to “this” instance

依然范特西╮ 提交于 2019-11-26 14:45:47
问题 The fat arrow can be used in different settings but it somehow doesn't always bind to the instance I want. 回答1: The fat arrow binds at 3 occasions when declaring a method when declaring a function within a method when declaring a function in global context 1. When declaring a method When the Coffeescript compiler encouters the following syntactical pattern within a class declaration class A somemethod: (paramlist) => This will yield the following code within the constructor of class A this

$('elems').each() with fat arrow

こ雲淡風輕ζ 提交于 2019-11-26 14:35:21
问题 I started to use ES6 fat arrow function notation and I really like it. But I am a little bit confused about it context. As far as I know, keyword this inside fat arrow function refers to context where the function is currently running. I wanted to do some simple jQuery iteration like: $('ul#mylist > li').each(() => { $(this).addClass('some-class-name'); }); But obviously this piece of code not working. How do I refer, inside fat arrow function, to current "LI" element in this specific code?

Using _ (underscore) variable with arrow functions in ES6/Typescript

感情迁移 提交于 2019-11-26 12:16:07
I came across this construct in an Angular example and I wonder why this is chosen: _ => console.log('Not using any parameters'); I understand that the variable _ means don't care/not used but since it is the only variable is there any reason to prefer the use of _ over: () => console.log('Not using any parameters'); Surely this can't be about one character less to type. The () syntax conveys the intent better in my opinion and is also more type specific because otherwise I think the first example should have looked like this: (_: any) => console.log('Not using any parameters'); In case it

Curly Brackets in Arrow Functions

别来无恙 提交于 2019-11-26 11:55:42
can someone, please explain the following: I'm following Dan Abramov's lectures & doing the exercises. The code works fine, however, the tests fail when the following particular function is written with curly brackets **{ }** . case 'toggleTodo' : return ( state.map( (one) => { oneTodo( one, action ) }) ); The same code works fine without curly brackets. case 'toggleTodo' : return ( state.map( (one) => oneTodo( one, action ) ) ); Here is the JsBin . Please refer to line 31 onwards. case 'toggleTodo' : return ( state.map( (one) => oneTodo( one, action ) ) ); is equal to: case 'toggleTodo' :

CoffeeScript, When to use fat arrow (=>) over arrow (->) and vice versa

≯℡__Kan透↙ 提交于 2019-11-26 11:46:28
问题 When building a class in CoffeeScript, should all the instance method be defined using the => (\"fat arrow\") operator and all the static methods being defined using the -> operator? 回答1: No, that's not the rule I would use. The major use-case I've found for the fat-arrow in defining methods is when you want to use a method as a callback and that method references instance fields: class A constructor: (@msg) -> thin: -> alert @msg fat: => alert @msg x = new A("yo") x.thin() #alerts "yo" x.fat

Arrow Function in Object Literal [duplicate]

假装没事ソ 提交于 2019-11-26 11:14:30
问题 This question already has an answer here: Are 'Arrow Functions' and 'Functions' equivalent / exchangeable? 1 answer Methods in ES6 objects: using arrow functions 3 answers I\'m trying to figure out why an arrow function in an object literal is called with window as this . Can someone give me some insight? var arrowObject = { name: \'arrowObject\', printName: () => { console.log(this); } }; // Prints: Window {external: Object, chrome: Object ...} arrowObject.printName(); And an object that