arrow-functions

JavaScript ES6: Test for arrow function, built-in function, regular function?

人走茶凉 提交于 2019-11-26 11:10:03
问题 Is there an elegant way to tell Harmony\'s slim arrow functions apart from regular functions and built-in functions? The Harmony wiki states that: Arrow functions are like built-in functions in that both lack .prototype and any [[Construct]] internal method. So new (() => {}) throws a TypeError but otherwise arrows are like functions Which means, you can test for arrow functions like: !(()=>{}).hasOwnProperty(\"prototype\") // true !(function(){}).hasOwnProperty(\"prototype\") // false But

ES6 immediately invoked arrow function

有些话、适合烂在心里 提交于 2019-11-26 10:13:29
问题 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. 回答1: You need to

Correct use of arrow functions in React

那年仲夏 提交于 2019-11-26 09:25:02
问题 I am using ReactJS with Babel and Webpack and using ES6 as well as the proposed class fields for arrow functions. I understand that arrow functions make things more efficient by not recreating the functions each render similar to how binding in the constructor works. However, I am not 100% sure if I am using them correctly. The following is a simplified section of my code in three different files. My code: Main.js prevItem = () => { console.log(\"Div is clicked\") } render(){ return (

this is undefined inside arrow function

旧城冷巷雨未停 提交于 2019-11-26 08:36:05
问题 I am trying to access this inside my arrow function: import myObject from \'../myObjectPath\'; export const myClass = Fluxxor.createStore({ initialize() { this.list = []; this.id = null; }, myOutsideFunction(variable1) { // here this in NOT undefined myObject.getMyList(this.id, (myList) => { // here this in undefined this.list = myList; } }); )}; But inside arrow function which in ma callback function this is undefined!! I am using babel to transpile the code: myOutsideFunction: function

Getting this as undefined when using arrow function

自作多情 提交于 2019-11-26 07:48:53
问题 I\'m using arrow functions and I\'m debugging with Chrome and Firefox Dev Tool. I am getting, this as undefined, even though the code still works. My assumption is, that it has something to do with source-maps. Here are the tools I use in order to build the my code: webpack (devtool: eval) babel-loader (es5 preset) typescript-loader 回答1: The problem is that the chrome debugger believes that the this in the source code refers to the run-time this , but this inside a arrow function in

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

时间秒杀一切 提交于 2019-11-26 06:48:30
问题 This question already has an answer here: What's the meaning of “=>” (an arrow formed from equals & greater than) in JavaScript? 13 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.

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

江枫思渺然 提交于 2019-11-26 05:37:34
问题 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

Syntax for async arrow function

六月ゝ 毕业季﹏ 提交于 2019-11-26 04:58:52
问题 I can mark a javascript function as \"async\" (i.e. returning a promise) with the async keyword. Like this: async function foo() { // do something } What is the equivalent syntax for arrow functions? 回答1: Async arrow functions look like this: const foo = async () => { // do something } Async arrow functions look like this for a single argument passed to it: const foo = async evt => { // do something with evt } The anonymous form works as well: const foo = async function() { // do something }

ES6 arrow functions not working on the prototype?

感情迁移 提交于 2019-11-26 03:40:28
问题 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:

Curly Brackets in Arrow Functions

家住魔仙堡 提交于 2019-11-26 03:34:47
问题 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. 回答1: