arrow-functions

Arrow function and `this`

泪湿孤枕 提交于 2019-12-02 16:50:27
问题 I am pasting a snippet from mozilla docs. An arrow function does not create its own this, the this value of the enclosing execution context is used. Thus, in the following code, the this within the function that is passed to setInterval has the same value as this in the enclosing function: function Person(){ this.age = 0; setInterval(() => { this.age++; // |this| properly refers to the person object }, 1000); } var p = new Person(); My confusion is when the above code is executed, the

Passing this from .call() to arrow function [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-12-01 22:57:25
问题 This question already has an answer here : Are 'Arrow Functions' and 'Functions' equivalent / exchangeable? (1 answer) Closed 2 years ago . I have an arrow function that I am trying to execute with call() . For the sake of simplification, as follows: Operational as expected const func = (e) => { console.log(e) } func.call(null, e) Hmm ... what's going on here? I would expect the following code to pass element into func as this . const func = (e) => { console.log(this) console.log(e) } func

nodejs arrow function with expression [duplicate]

谁都会走 提交于 2019-12-01 21:47:21
This question already has an answer here: ECMAScript 6 arrow function that returns an object 6 answers According to the documentation , you can return an expression from an arrow function: (param1, param2, …, paramN) => expression // equivalent to: => { return expression; } but this doesn't seem to work as I would expect (nodejs 4.2.3) > [1,2,3].map(i => i); [ 1, 2, 3 ] > [1,2,3].map(i => {}); [ undefined, undefined, undefined ] Shouldn't the 2nd example return 3 empty objects? Or am I missing something? According to the docs , the body of fat arrow function can be written as either a single

react native arrow functions and if statements

独自空忆成欢 提交于 2019-12-01 13:48:06
I have just been schooled on arrow functions, and how they can aid with visibility when you start using sub-functions, here: react native and globally accessible objects I am not sure if this is different for "if" statements, but I can't get this to work at all. The issue: myFunction() { console.log('Welcome Flag: ' + this.props.welcomeFlag); if (this.props.welcomeFlag == false) { this.props.dispatch(setWelcomeFlag(true)); showMessage('Welcome back, ' + this.props.userName + '!', { duration: 3000 }); } } In this example, the console logs the initial value of welcomeFlag, which is "false". I

How do I refer to actual 'this' inside CoffeeScript fat-arrow callback?

早过忘川 提交于 2019-12-01 11:26:50
The title says it all. When I use the fat-arrow in CoffeeScript, it stores this first before calling the function. For example: class myClass constructor: -> element = $ "#id" element.click -> @myMethod(@value) return return myMethod: (c)-> window.console.log(c) return would yield var myClass; myClass = (function() { function myClass() { var element; element = $("#id"); element.click(function() { this.myMethod(this.value); }); return; } myClass.prototype.myMethod = function(c) { window.console.log(c); }; return myClass; })(); Now on line#8 of JavaScript, this.myMethod is wrong. In this scope,

Angular2 subscribe understand arrow function

孤人 提交于 2019-12-01 04:04:36
I try to understand arrow functions of typescript by the example of Angular 2 Observable subscribe method. Could somebody explain me: I have this code which works: this.readdataservice.getPost().subscribe( posts => { this.posts = posts; } ); but should it be the same if I use this? But this doesn't work. this.readdataservice.getPost().subscribe( function (posts) { this.posts = posts; } ); JS by default executes functions in the scope of the caller. If you pass a function around to be called somewhere else, this points to the caller. In your code you pass the function to the observable via the

Expected to return a value in arrow; function array-callback-return. Why?

℡╲_俬逩灬. 提交于 2019-12-01 03:49:13
I'm having some issues understanding why I'm getting a compile warning on this piece of my react code fetch('/users') .then(res => res.json()) .then(data => { data.map(users => { console.log(users); }); }); The warning I'm getting is Expected to return a value in arrow function array-callback-return However I'm still get the json object values from my /users , and they are printed to the console individually. The object is: { id: 1, username: "Foo" }, { id: 2, username: "Bar" } Am I missing a return statement, or am I missing something with how map returns values after a .then() ? I'm unclear

How to using ES6 Arrow function to realize Immediately-Invoked Function Expression (IIFE))? [closed]

倖福魔咒の 提交于 2019-12-01 02:01:34
How to using ES6 Arrow function to realize IIFE Immediately-Invoked Function Expression ? Here is my demo codes, and it had tested passed! // ES 6 + IIFE (() => { let b = false; console.log(`b === ${b}!`); const print = `print()`; if(window.print){ b = true; console.log(`b === ${b}!`); } let x = () => { if(b){ console.log(`Your browser support ${print} method.`); }else{ alert(`Your browser does not support ${print} method.`); console.log(`Your browser does not support ${print} method.`); }; } x(); })(); const dcs = `IIFE: Douglas Crockford's style`; // ES 5 + IIFE is OK (function(){ alert(

Typescript: How to call method defined with arrow function in base class using super keyword in child class?

試著忘記壹切 提交于 2019-11-30 18:44:56
Given class BaseClass{ count:number=0; public someMethod=():void =>{ this.count++; } } class ChildClass extends BaseClass{ public someMethod=():void=>{ super.someMethod(); //Do more work here. } } I receive the error message: Only public methods of the base class are accessible via the 'super' keyword. @Basarat provides some information here but this seems like a real hack to the language. typescript arrow operator to define a function on prototype How might this be done while preserving contextual use of 'this'? Am I using arrow functions properly or should they really only be used as a

Classes with static arrow functions

天涯浪子 提交于 2019-11-30 18:34:52
I'm currently implementing the static land specification (an alternative of fantasy land). I want to not only use plain objects as types but also ES2015 classes with static methods. I've implemented these static methods as arrow functions in curried form instead of normal functions. However, this isn't possible with ES2015 classes: class List extends Array { static map = f => xs => xs.map(x => f(x)) static of = x => [x] } My map doesn't need its own this , because it is merely a curried function on the List constructor. To make it work I have to write static map(f) { return xs => xs.map(x => f