arrow-functions

ES6 Arrow Functions and Promise Chaining condensed syntax explanation

十年热恋 提交于 2020-06-17 15:53:31
问题 In the following code block, can someone please provide links or an explanation for the condensed alert statement syntax. I understand the preceding expanded equivalent code that is commented out and contains the message parameter. However, I cannot find a reference to the syntax for omitting the message parameter: let timeoutPromise = new Promise((resolve, reject) => { setTimeout(() => { resolve('Success!'); }, 2000); }); /* timeoutPromise.then(message => { alert(message); }) */

“this” keyword behaves differently when using arrow functions with jQuery callbacks [duplicate]

南楼画角 提交于 2020-05-24 02:52:30
问题 This question already has answers here : Are 'Arrow Functions' and 'Functions' equivalent / exchangeable? (3 answers) Closed 2 years ago . I have a table with multiple rows, and on each row, there is an edit and delete button. In short, when an edit button is triggered with class .edit , a form pops-up. Along with the class name, I have also included a unique id like id="edit-32" , where the number changes depending on what row the user clicks on. The problem I am trying to solve is to

really confused by “enclosing scope” of javascript es6 arrow function

谁说胖子不能爱 提交于 2020-05-08 03:39:07
问题 I did a lot research online, read many posts including MDN and so on. I understand that for traditional defined functions, "this" within functions is defined by objects calling/invoking them (and several different cases, object literal, new constructor, event handler, etc.). I understand that for arrow functions, "this" is defined lexically, by the enclosing context/scope, not by objects invoking them (though we can use a traditionally defined function (say, A) to wrap arrow functions (say, B

Combine 2 lists into one using map & arrow function

依然范特西╮ 提交于 2020-02-25 02:09:07
问题 The bounty expires in 12 hours . Answers to this question are eligible for a +50 reputation bounty. d_k is looking for an answer from a reputable source . I have 2 lists and I want to combine them so that I can populate them into a list. I know this can be done using nested for loops but, I'm trying to avoid for loops because of the amount of data I'll have to loop on. I would like to achieve this using the arrow functions or anything else. List One: let fields = [ { field: "Name", fieldType:

Lexical context of 'this' in Nested Object Literals

巧了我就是萌 提交于 2020-02-24 12:08:51
问题 I'm asking because I've read similar (but not equal) questions about this issue. As far as I understand, I get why this code would NOT work: let myObj = { name: 'inner text', myFunction: () => { console.log("before text " + this.name + " after text"); } } myObj.myFunction(); Reason: the context where the arrow function is created ( myObj ) belongs to the global scope (which is window or undefined depending on strict mode and so on). I get that. But in the case of nested object literals ,

Lexical context of 'this' in Nested Object Literals

◇◆丶佛笑我妖孽 提交于 2020-02-24 12:07:35
问题 I'm asking because I've read similar (but not equal) questions about this issue. As far as I understand, I get why this code would NOT work: let myObj = { name: 'inner text', myFunction: () => { console.log("before text " + this.name + " after text"); } } myObj.myFunction(); Reason: the context where the arrow function is created ( myObj ) belongs to the global scope (which is window or undefined depending on strict mode and so on). I get that. But in the case of nested object literals ,

Rewriting an anonymous function in php 7.4

半城伤御伤魂 提交于 2020-01-31 15:07:22
问题 There is the following anonymous recursive function: $f = function($n) use (&$f) { return ($n == 1) ? 1 : $n * $f($n - 1); }; echo $f(5); // 120 I try to rewrite to version 7.4, but there is an error, please tell me what I'm missing? $f = fn($n) => ($n == 1) ? 1 : $n * $f($n - 1); echo $f(5); Notice: Undefined variable: f Fatal error: Uncaught Error: Function name must be a string 回答1: Just like Barmar said, you can't use $f from the outside scope, because when the implicit binding takes

ESCMAScript 6 arrow functions - parentheses around parameter

僤鯓⒐⒋嵵緔 提交于 2020-01-30 08:33:05
问题 I'm new to javascript and cannot understand simple thing - what is the difference between ...(x) => { return x*2} and ...x => { return x*2} //(just for example, may not work) Can someone explain or give link for description? 回答1: The parenthesis around input arguments ( x in this case) are only required when there are two or more input arguments. With just one (as you've shown here), the two statements are identical. (x) => { return x * 2; } is the same as x => { return x * 2; } But, (x, y) =

Arrow vs classic method in ES6 class

情到浓时终转凉″ 提交于 2020-01-26 20:13:52
问题 Is there any reason to write classic syntax of ES6 methods? class MyClass { myMethod() { this.myVariable++; } } When I use myMethod() as callback on some event, I must write something like this (in JSX): // Anonymous function. onClick={() => { this.myMethod(); }} // Or bind this. onClick={this.myMethod.bind(this)} But if I declare method as arrow function: class MyClass { myMethod = () => { this.myVariable++; } } than I can write just (in JSX): onClick={this.myMethod} 回答1: The feature you are

Use arrow functions in jQuery plugin

无人久伴 提交于 2020-01-25 20:58:46
问题 I am writing a jQuery plugin. But it doesn't seem to work when I use arrow function to extend jQuery. This works : $.fn.extend({ func: function (params) { var ob = $(this); var selector = $(this).selector; var defaults = { }; params = $.extend(defaults, params); generate(ob, selector, params); } }); But when I try to use arrow function, it returns me the window object : $.fn.extend({ func: (params) => { var ob = $(this); // returns window object var selector = $(this).selector; var defaults =