arrow-functions

JavaScript arrow syntax: What does the equals sign on the right hand side mean?

为君一笑 提交于 2021-02-11 12:15:38
问题 I am studying JavaScript syntax. I occasionally see a pattern that confuses me: an equals sign on the right hand side of the arrow. For example, something like this: .then(data => myVariable = data) I don't know what is going on in that syntax. It looks like it is taking the value of data and assigning it to the variable named myVariable . Can somebody explain this? 回答1: You're right. It's an arrow function (without an accompanying block) that "returns" an assignment expression - in effect

Passing parameters to a callback function using arrow function

烂漫一生 提交于 2021-02-08 12:37:13
问题 I know this is a duplicated question with ES5, but I am looking for the syntax with ES6 arrow function. My code below: fetchItems = (callback) => { //After ajax success callback(response); } const myParams = {name:"John"} this.fetchItems((res) => { console.log(res.data); }); For the above scenario, I want to pass some parameters( myParams ) along with the function call, how can I achieve that? 回答1: You can do that: const fetchItems = (callback, ...params) => { //Do whatever you want with the

Arrow function and this inside a constructor function

a 夏天 提交于 2021-02-07 04:28:45
问题 I have read this paragraph about the this keyword : https://bonsaiden.github.io/JavaScript-Garden/#function.this In this first case this refers to global objet, and it seems totally normal because when we have an arrow function, it automatically bind this with the one in the outer scope. var obj = { foo : () => console.log(this) } console.log(obj); obj.foo() However, I'm not able to explain the following behavior : function bar(){ this.foo = () => console.log(this) } var obj = new bar()

Converting an Arrow Style function to “function” style

≡放荡痞女 提交于 2021-02-05 06:36:43
问题 I have a function like this: const jsonObject = {a: {b: 'c'}}; const x = 'a.b'; const properties = x.split('.'); const item = properties.reduce((obj, prop) => obj && obj[prop], jsonObject); console.log(item); // prints 'c; This function, dynamically traverses the jsonObject and prints the value. This works fine, but this style of declaration doesn't support my environment. So I wan trying to convert this to function style declaration something like this: const item = properties.reduce

Converting an Arrow Style function to “function” style

倖福魔咒の 提交于 2021-02-05 06:36:27
问题 I have a function like this: const jsonObject = {a: {b: 'c'}}; const x = 'a.b'; const properties = x.split('.'); const item = properties.reduce((obj, prop) => obj && obj[prop], jsonObject); console.log(item); // prints 'c; This function, dynamically traverses the jsonObject and prints the value. This works fine, but this style of declaration doesn't support my environment. So I wan trying to convert this to function style declaration something like this: const item = properties.reduce

How to bind 'this' to an object arrow function?

筅森魡賤 提交于 2021-01-29 12:30:22
问题 Let us suppose we have an object profile with properties name and getName method (arrow function). profile = { name: 'abcd', getName: () => { console.log(this.name); } } I want to call getName method by keeping the arrow function intact, and not changing it to regular function. How can I get the output abcd by calling getName() . You can add expressions inside getName . Will call() or bind() help? If so, how? DO NOT CHANGE THE ARROW FUNCTION TO REGULAR FUNCTION -- EDITED -- I just want to ask

Override Arrow Function in Child Class

血红的双手。 提交于 2021-01-29 00:13:04
问题 I use arrow functions in my classes instead of methods because they allow me to use the "this" keyword, and keep the "this" meaning the class itself, rather than the method. I have an abstract class that defines an empty arrow function that is to be overridden by the child class. Correction, it was originally this: abstract _configureMultiselect(): void; but trying to override that with an arrow function caused this: Class 'CategorySelect' defines instance member function '

Override Arrow Function in Child Class

依然范特西╮ 提交于 2021-01-29 00:11:50
问题 I use arrow functions in my classes instead of methods because they allow me to use the "this" keyword, and keep the "this" meaning the class itself, rather than the method. I have an abstract class that defines an empty arrow function that is to be overridden by the child class. Correction, it was originally this: abstract _configureMultiselect(): void; but trying to override that with an arrow function caused this: Class 'CategorySelect' defines instance member function '

Override Arrow Function in Child Class

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-29 00:10:48
问题 I use arrow functions in my classes instead of methods because they allow me to use the "this" keyword, and keep the "this" meaning the class itself, rather than the method. I have an abstract class that defines an empty arrow function that is to be overridden by the child class. Correction, it was originally this: abstract _configureMultiselect(): void; but trying to override that with an arrow function caused this: Class 'CategorySelect' defines instance member function '

Arrow Function Hoisting in Node? [duplicate]

淺唱寂寞╮ 提交于 2021-01-27 06:50:04
问题 This question already has answers here : Are variables declared with let or const hoisted? (6 answers) Closed 1 year ago . I'm having a bit of trouble understanding why my code works. I'm expecting a reference error, but everything works fine. My code: const functionA = () => { let bResult = functionB(); console.log("Function A " + bResult); }; const functionB = () => { return "Function B"; }; functionA(); I get this output (no errors); λ node test.js Function A Function B As I understand it,