arrow-functions

ES6 immediately invoked arrow function

蹲街弑〆低调 提交于 2019-11-27 02:46:49
Why does this work in a Node.js console (tested in 4.1.1 and 5.3.0) but doesn't work in the browser (tested in Chrome)? This code block should create and invoke an anonymous function that logs Ok . () => { console.log('Ok'); }() Also, while the above works in Node, this does not work: n => { console.log('Ok'); }() Nor this: (n) => { console.log('Ok'); }() What's odd is that when the parameter is added it actually throws a SyntaxError at the immediately-invoking part. void You need to make it a function expression instead of function definition which doesnt need a name and makes it a valid

ES6 functions, arrow functions and 'this' in an ES6 class [duplicate]

心不动则不痛 提交于 2019-11-26 22:56:44
问题 This question already has answers here : Should I write methods as arrow functions in Angular's class (3 answers) Arrow vs classic method in ES6 class (1 answer) Closed last year . class App extends Component { constructor(props) { ... } onChange = (e) => this.setState({term: e.target.value}) onSubmit(e){ e.preventDefault(); const api_key = "C1hha1quJAQZf2JUlK"; const url = `http://api.giphy.com/v1/gifs/search?q=${this.state.term}&api_key=${api_key}`; } render() { return ( <div> <form

Why is `throw` invalid in an ES6 arrow function?

懵懂的女人 提交于 2019-11-26 22:24:51
问题 I'm just looking for a reason as to why this is invalid: () => throw 42; I know I can get around it via: () => {throw 42}; 回答1: If you don't use a block ( {} ) as body of an arrow function, the body must be an expression: ArrowFunction: ArrowParameters[no LineTerminator here] => ConciseBody ConciseBody: [lookahead ≠ { ] AssignmentExpression { FunctionBody } But throw is a statement, not an expression. In theory () => throw x; is equivalent to () => { return throw x; } which would not be valid

Can I use ES6's arrow function syntax with generators? (arrow notation)

≡放荡痞女 提交于 2019-11-26 21:48:32
ie how do I express this: function *(next) {} with arrows. I've tried all the combinations I could think of, and I can't find any documentation on it. (currently using node v0.11.14) Can I use ES6's arrow function syntax with generators? You can't. Sorry. According to MDN The function* statement ( function keyword followed by an asterisk) defines a generator function. From a spec document (my emphasis): The function syntax is extended to add an optional * token: FunctionDeclaration: "function" "*"? Identifier "(" FormalParameterList? ")" "{" FunctionBody "}" CoderPi The difference between

This values for arrow functions [duplicate]

空扰寡人 提交于 2019-11-26 21:35:49
问题 This question already has an answer here: Methods in ES6 objects: using arrow functions 3 answers How does the “this” keyword in Javascript act within an object literal? [duplicate] 4 answers I am trying to understand arrow functions in ECMAScript 6. This is the definition I came across while reading: Arrow functions have implicit this binding, which means that the value of the this value inside of an arrow function is aways the same as the value of this in the scope in which the arrow

What does arrow function '() => {}' mean in Javascript? [duplicate]

淺唱寂寞╮ 提交于 2019-11-26 19:03:48
This question already has an answer here: What's the meaning of “=>” (an arrow formed from equals & greater than) in JavaScript? 12 answers I was reading the source for ScrollListView and in several places I see the use of () => {} . Such as on line 25, this.cellReorderThreshold = () => { var ratio = (this.CELLHEIGHT*this.cellsWithinViewportCount)/4; return ratio < this.CELLHEIGHT ? 0 : ratio; }; line 31, this.container.addEventListener('scroll', () => this.onScroll(), false); line 88. resizeTimer = setTimeout(() => { this.containerHeight = this.container.offsetHeight; }, 250); Is this a

Can I use arrow function in constructor of a react component?

北慕城南 提交于 2019-11-26 17:52:42
This question is similar to When using React Is it preferable to use fat arrow functions or bind functions in constructor? but a little bit different. You can bind a function to this in the constructor, or just apply arrow function in constructor. Note that I can only use ES6 syntax in my project. 1. class Test extends React.Component{ constructor(props) { super(props); this.doSomeThing = this.doSomeThing.bind(this); } doSomething() {} } 2. class Test extends React.Component{ constructor(props) { super(props); this.doSomeThing = () => {}; } } What's the pros and cons of these two ways? Thanks.

ES6 arrow functions not working on the prototype?

允我心安 提交于 2019-11-26 17:43:33
When ES6 Arrow functions don't seem to work for assigning a function to an object with prototype.object. Consider the following examples: function Animal(name, type){ this.name = name; this.type = type; this.toString = () => `${this.name} is a ${this.type}`; } var myDog = new Animal('Max', 'Dog'); console.log(myDog.toString()); //Max is a Dog Using the arrow function explicitly in the object definition works, but using the arrow functions with the Object.prototype syntax does not: function Animal2(name, type){ this.name = name; this.type = type; } Animal2.prototype.toString = () => `${this

Performance penalty of creating handlers on every render with react-hooks

廉价感情. 提交于 2019-11-26 17:22:47
问题 I'm currently very amazed about the use cases of the new react hooks API and what you can possibly do with it. A question that came up while experimenting was how expensive it is to always create a new handler function just to throw it away when using useCallback . Considering this example: const MyCounter = ({initial}) => { const [count, setCount] = useState(initial); const increase = useCallback(() => setCount(count => count + 1), [setCount]); const decrease = useCallback(() => setCount

How to translate 'this' in D3 JavaScript to TypeScript?

旧街凉风 提交于 2019-11-26 17:19:40
问题 I know that 'this' in JavaScript has a different meaning than in in TypeScript, as per this article 'this' in TypeScript. I have the following code in JavaScript used to create a thicker stroke on the selected node, and give all other nodes a smaller stroke. node.on('click', function (d) { d3.selectAll('circle').attr('stroke-width', 1.5); d3.select(this).select('circle').attr('stroke-width', 5); }) In TypeScript I have this.node.on('click', (d:any) => { this.node.selectAll('circle').attr(