arrow-functions

Arrow function “expression expected” syntax error

拜拜、爱过 提交于 2019-11-28 23:34:00
问题 I want to transform this code: var formatQuoteAmount = function (tx) { return Currency.toSmallestSubunit(tx.usd, 'USD'); }; var quoteAmounts = res.transactions.map(formatQuoteAmount); into an anonymous arrow function. I've written this: var quoteAmounts = res.transactions.map(tx => Currency.toSmallestSubunit(tx.usd, 'USD')); I get expression expected syntax error at the arrow. I looked up the default syntax here and seems like the syntax of my code is correct. Any ideas what the problem might

How do I write an arrow function in ES6 recursively?

烂漫一生 提交于 2019-11-28 21:09:05
Arrow functions in ES6 do not have an arguments property and therefore arguments.callee will not work and would anyway not work in strict mode even if just an anonymous function was being used. Arrow functions cannot be named, so the named functional expression trick can not be used. So... How does one write a recursive arrow function? That is an arrow function that recursively calls itself based on certain conditions and so on of-course? Writing a recursive function without naming it is a problem that is as old as computer science itself (even older, actually, since λ-calculus predates

What are ES6 arrow functions, how do they work? [duplicate]

蹲街弑〆低调 提交于 2019-11-28 13:01:41
问题 This question already has an answer here: Are 'Arrow Functions' and 'Functions' equivalent / exchangeable? 1 answer I am curious about ES6 arrow functions (fat arrow functions). Are they simply syntactic sugar derived from CoffeeScript, or is there more to them than meets the eye? 回答1: ES6 Arrow functions in depth One of the prettiest features of ES6, it could easily win a beauty contest, if such a contest would be held. What many people don’t know is that the arrow function is not simply a

ES6 class methods not returning anything inside forEach loop

萝らか妹 提交于 2019-11-28 12:30:42
问题 For some reason the method getTwo() inside the PollClass won't return 2 but undefined . If I put the return statement outside the .forEach() loop a value does get returned however. class Poll { constructor(name) { this.name = name; this.nums = [1, 2, 3]; } getTwo() { this.nums.forEach(num => { if (num === 2) return num; }) } } const newPoll = new Poll('random name'); console.log(newPoll.getTwo()); // returns undefined, not 2 Is this an issue with closure, ES 6, or a whole other issue? 回答1: An

Arrow functions vs Fat arrow functions

不羁岁月 提交于 2019-11-28 11:58:13
I've found on the internet about both names, arrow functions and fat arrow functions but no information about what is different between them. Are there any differences? Such a question requires a bit of explanation... ECMAScript 5 In ES5 specification, there were no arrow functions at all. It was then common to use traditional function expressions like so: // Example n°1 var myFunction = function () { return 'Hello!'; }; // Example n°2 var obj = { myFunction: function () { return 'Hello!'; } }; // Example n°3 var arr = ['foo', 'bar']; arr.map(function (item) { return 'Hello, ' + item + '!'; };

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

时光毁灭记忆、已成空白 提交于 2019-11-28 10:41:24
This question already has an answer here: Official information on `arguments` in ES6 Arrow functions? 2 answers 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? From the spec : Any reference to arguments , super , this , or new.target within an ArrowFunction must resolve to a binding in a

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

不羁的心 提交于 2019-11-28 01:21:01
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('stroke-width', 1.5); [this is where I need help].select('circle').attr('stroke-width', 5); } Gerardo

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

送分小仙女□ 提交于 2019-11-28 01:07:58
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 Arrow Functions " gives a very good explanation of why this is not bound as expected. The article

This values for arrow functions [duplicate]

巧了我就是萌 提交于 2019-11-27 23:55:30
This question already has an answer here: Methods in ES6 objects: using arrow functions 4 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 function is defined! According to the definition, I believe this for an arrow function should contain the

Why isn't this JavaScript syntax supported in Google Chrome?

心已入冬 提交于 2019-11-27 23:41:29
问题 I initiated a JavaScript/jQuery click listener like this: $("#test").on("click", () => { console.log("test"); }); This piece of code works perfectly fine in Firefox but in Chrome this seems to give me a Syntax error. Why is this, since this looks like 'ok' syntax to me. You can test this quickly in the console by doing var a = () => {return 0;} a(); In Firefox 27.0.1 this returns 0 In Chrome it returns SyntaxError: Unexpected token ) 回答1: The fat arrow is a feature of ES6 (now officially