arrow-functions

Official information on `arguments` in ES6 Arrow functions?

夙愿已清 提交于 2019-11-26 03:20:29
问题 (() => 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.

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

送分小仙女□ 提交于 2019-11-26 02:53:45
问题 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

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

有些话、适合烂在心里 提交于 2019-11-26 01:47:45
问题 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

How do I write a named arrow function in ES2015?

梦想的初衷 提交于 2019-11-26 01:36:13
问题 I have a function that I am trying to convert to the new arrow syntax in ES6 . It is a named function: function sayHello(name) { console.log(name + \' says hello\'); } Is there a way to give it a name without a var statement: var sayHello = (name) => { console.log(name + \' says hello\'); } Obviously, I can only use this function after I have defined it. Something like following: sayHello = (name) => { console.log(name + \' says hello\'); } Is there a new way to do this in ES6 ? 回答1: How do I

Why shouldn't JSX props use arrow functions or bind?

有些话、适合烂在心里 提交于 2019-11-26 01:32:10
问题 I\'m running lint with my React app, and I receive this error: error JSX props should not use arrow functions react/jsx-no-bind And this is where I\'m running the arrow function (inside onClick ): {this.state.photos.map(tile => ( <span key={tile.img}> <Checkbox defaultChecked={tile.checked} onCheck={() => this.selectPicture(tile)} style={{position: \'absolute\', zIndex: 99, padding: 5, backgroundColor: \'rgba(255, 255, 255, 0.72)\'}} /> <GridTile title={tile.title} subtitle={<span>by <b>{tile

Why doesn&#39;t my arrow function return a value?

假如想象 提交于 2019-11-26 01:23:45
问题 I have an arrow function that looks like this (simplified): const f = arg => { arg.toUpperCase(); }; But when I call it, I get undefined : console.log(f(\"testing\")); // undefined Why? Example: const f = arg => { arg.toUpperCase(); }; console.log(f(\"testing\")); ( Note: This is meant to be a clean, canonical dupetarget for the specific issue with arrow functions above.) 回答1: When you use the function body version of an arrow function (with {} ), there is no implied return . You have to

What do multiple arrow functions mean in javascript?

可紊 提交于 2019-11-26 01:19:05
问题 I have been reading a bunch of react code and I see stuff like this that I don\'t understand: handleChange = field => e => { e.preventDefault(); /// Do something here } 回答1: That is a curried function First, examine this function with two parameters … const add = (x, y) => x + y add(2, 3) //=> 5 Here it is again in curried form … const add = x => y => x + y Here is the same 1 code without arrow functions … const add = function (x) { return function (y) { return x + y } } Focus on return It

Arrow function without curly braces

若如初见. 提交于 2019-11-26 01:07:21
问题 I\'m new to both ES6 and React and I keep seeing arrow functions. Why is it that some arrow functions use curly braces after the fat arrow and some use parentheses? For example: const foo = (params) => ( <span> <p>Content</p> </span> ); vs. const handleBar = (e) => { e.preventDefault(); dispatch(\'logout\'); }; Thanks for any help! 回答1: The parenthesis are returning a single value, the curly braces are executing multiple lines of code. Your example looks confusing because it's using JSX which

Arrow Functions and This [duplicate]

感情迁移 提交于 2019-11-25 22:35:51
问题 This question already has answers here : Methods in ES6 objects: using arrow functions (3 answers) Closed 2 years ago . I\'m trying out ES6 and want to include a property inside my function like so var person = { name: \"jason\", shout: () => console.log(\"my name is \", this.name) } person.shout() // Should print out my name is jason However, when I run this code console only logs my name is . What am I doing wrong? 回答1: Short answer: this points at the nearest bound this - in the code

When should I use `return` in es6 Arrow Functions?

牧云@^-^@ 提交于 2019-11-25 22:25:00
问题 The new es6 arrow functions say return is implicit under some circumstances: The expression is also the implicit return value of that function. In what cases do I need to use return with es6 arrow functions? 回答1: Jackson has partially answered this in a similar question: Implicit return, but only if there is no block. This will result in errors when a one-liner expands to multiple lines and the programmer forgets to add a return . Implicit return is syntactically ambiguous. (name) => {id: