arrow-functions

Getting this as undefined when using arrow function

别说谁变了你拦得住时间么 提交于 2019-11-27 09:32:41
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 The problem is that the chrome debugger believes that the this in the source code refers to the rune-time this , but this inside a arrow function in typescript source code is actually transformed to _this , so it's showing you the wrong object. This is why it's only

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

痴心易碎 提交于 2019-11-27 09:27:49
The fat arrow can be used in different settings but it somehow doesn't always bind to the instance I want. 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.somemethod = __bind(this.somemethod, this); That is the definition for that instance is overwriting the

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

故事扮演 提交于 2019-11-27 09:13:39
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? The each() method supplies two parameters to the callback-function. They are current index and the

Are arrow functions optimized like named functions?

不问归期 提交于 2019-11-27 08:06:55
问题 I was watching a NodeJS Interactive talk and the guy speaking was saying how anonymous functions were bad one of the reasons being that if they have no name, the VM cannot optimize the function based on how frequently it's used because its nameless. So if a function with a name is called random.Async('Blah', function randomFunc() {}); randomFunc can be optimized as where a function like: random.Async('Blah', function(cb) {}); This will not be optimized because it's anonymous, nameless. So I

Correct use of arrow functions in React

我怕爱的太早我们不能终老 提交于 2019-11-27 06:58:44
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 ( <SecondClass prevItem={this.prevItem} /> ) } SecondClass.js <ThirdClass type="prev" onClick={()=>this.props

Do ES6 arrow functions have their own arguments or not? [duplicate]

谁说胖子不能爱 提交于 2019-11-27 05:53:35
问题 This question already has answers here : Official information on `arguments` in ES6 Arrow functions? (2 answers) Closed 4 years ago . I don’t know whether arrow functions bind arguments to a lexical scope or not. Take a look at this example (the same concept can be used for this ): var b = function() { return () => console.log(arguments); }; b(1,2,3)(4,5,6); // different result of chrome vs FF. When I run this on Chrome, I get [1,2,3] , but on Firefox, I get [4,5,6] . What’s going on? 回答1:

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

六眼飞鱼酱① 提交于 2019-11-27 05:53: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? 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() #alerts "yo" fn = (callback) -> callback() fn(x.thin) #alerts "undefined" fn(x.fat) #alerts "yo" fn(-> x

Arrow Function in Object Literal [duplicate]

给你一囗甜甜゛ 提交于 2019-11-27 05:03:10
This question already has an answer here: Are 'Arrow Functions' and 'Functions' equivalent / exchangeable? 1 answer Methods in ES6 objects: using arrow functions 4 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 works as expected: var functionObject = { name: 'functionObject', printName: function() { console.log(this);

D3.js v4: Access current DOM element in ES6 arrow function event listener

北战南征 提交于 2019-11-27 04:46:03
问题 In D3.js v4, when registering an event listener through a traditional callback function, this references the current DOM element: d3.select("div").on('mouseenter', function() { d3.select(this).text("Yay"); }); ES6 offers arrow functions, which IMHO make D3.js code a lot more readable because they are very concise. However, traditional callbacks cannot blindly be replaced with arrow functions: d3.select("div").on('mouseenter', () => { d3.select(this); // undefined }); The article "On D3 and

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

房东的猫 提交于 2019-11-27 04:24:58
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 the test will also return true for any built-in function, e.g. setTimeout or Math.min . It sort of works in