arrow-functions

Using arrow functions with .call()? “This” varies based on if in devtools or iojs locally? Should one avoid using arrows with .call?

本秂侑毒 提交于 2019-12-12 20:58:15
问题 I've been running the below code and I notice that when running in devtools, obj.hi = 'default', while when running with iojs 3.3.1 with harmony arrow functions as an arg on my machine, obj.hi = 'foo'. "This" is the window when in devtools, while "this" is the object in iojs. Is it generally good practice to avoid using arrows with .call to avoid this? 'use strict' let obj = { hi: "default" } let foobar = () => { console.log(this) this.hi = "foo" } foobar.call(obj) console.log(obj) 回答1: See

Compile arrow functions into regular functions with TypeScript

两盒软妹~` 提交于 2019-12-12 16:23:44
问题 Quite a simple question, but I haven't found an answer yet anywhere: Is there some switch to make TypeScript compile arrow functions into plain JavaScript functions? I use them a lot in my code, and I don't want to rewrite everything. But I lately realized, that IE doesn't support them. I already tried to switch the script version to ES5, but then my code won't compile anymore, because I'm using "filter" as well, which doesn't seem to be a part of it. However, I don't know, if that would do

Function.prototype.call does not set this at Arrow Function [duplicate]

老子叫甜甜 提交于 2019-12-12 12:47:33
问题 This question already has an answer here : Are 'Arrow Functions' and 'Functions' equivalent / exchangeable? (1 answer) Closed 3 years ago . Note, Related Value of this inside object method? Given var obj = { property: 5, func1: function () { console.log(this.property); }, func2: () => { console.log(this.property); } } this would be Window at obj.func2() . When tried setting this to obj using Function.prototype.call() , this was still Window var obj = { property: 5, func1: function () {

Is it possible to use arrow functions in classes with ES6?

风格不统一 提交于 2019-12-12 09:35:35
问题 My question is very simple. If I have a class in ES6 is it possible to use an arrow function within it? import React, { Component } from 'react'; export default class SearchForm extends Component { state = { searchText: '' } onSearchChange = e => { this.setState({ searchText: e.target.value }); } handleSubmit = e => { e.preventDefault(); this.props.onSearch(this.query.value); e.currentTarget.reset(); } render() { return ( <form className="search-form" onSubmit={this.handleSubmit} > <label

CoffeeScript: How to use both fat arrow and this?

我是研究僧i 提交于 2019-12-12 07:09:26
问题 I have a coffeescript class that has some jquery event listeners. I would like to use the fat arrow => to avoid having to reference the class, but I still need a reference to the element that would usually be used with this . How can I use both? class PostForm constructor: -> $('ul.tabs li').on 'click', => tab = $(this) @highlight_tab(tab) @set_post_type(tab.attr('data-id')) highlight_tab: (tab)-> tab.addClass 'active' set_post_type: (id) -> $('#post_type_id').val(id) 回答1: CoffeeScript links

Arrow functions inside map in render. React

故事扮演 提交于 2019-12-12 04:38:52
问题 I'd like to know if there is any performance difference between passing an arrow function to a map function inside render, and passing a reference to that arrow function, like so: render { const { users } = this.props; //Arrow function passed to map, in render. const userList= users.map((user) => <User name={ user.name } />); return( <div> {userList} </div> ); } vs makeUser = (user) => <User name={ this.props.user.name } /> render { const { users } = this.props; //Passing the arrow function

Why arrow function is not passing arguments?

穿精又带淫゛_ 提交于 2019-12-12 04:23:07
问题 I'm using angular w/ rxjs to observe user events on the interface. However, I'm having this really simple problem with passing arguments to a method in an arrow function. Here is the problem: This is not working : searchterm is not being passed to this.searchFacilities ngOnInit() { this.searchTerm$.subscribe(searchterm => this.searchFacilities);/**Not working here**/ } searchFacilities(term: string){ console.log(term); this.facilityservice.searchFacilities(term) .subscribe(results => this

Curried function where ES7 property initializers aren't enabled

折月煮酒 提交于 2019-12-12 02:08:12
问题 I have the following arrow function in a so called curried format, taken from this SO answer: const getIntervals = n=> availability=> {} I need to use it in a React component, written in ES6 syntax, that cannot handle ES7 property initializers due to the implementation. "Normally" a function in a class context in React has this simple style: myFunction () {} But would I convert the getIntervals function to this style? 回答1: Just define getIntervals as a regular method, but have it return your

ES6 giving SyntaxError: Unexpected token ) [duplicate]

风格不统一 提交于 2019-12-12 01:37:40
问题 This question already has answers here : ECMAScript 6 features available in Node.js 0.12 (2 answers) Closed 3 years ago . Here is my Node.js script: pDownload("http://serv/file", "localfile") .then( ()=> console.log('downloaded file no issues...')) .catch( e => console.error('error while downloading', e)); function pDownload(url, dest){} // Yes empty, for now When executing on Node.js v0.10.25 (default on Ubuntu 2015.10) I receive this syntax error about the parenthesis: /home/nico/main.js:2

typescript arrow operator to define a function on prototype

风格不统一 提交于 2019-12-12 00:58:27
问题 As shown in this example, the assignment to a and defining b results in different function type. export module A { export class Test { constructor(){} a =(x) => { return Math.sin(x); } b (x) : any { return Math.sin(x); } } } this results in following js var Test = (function () { function Test() { this.a = function (x) { return Math.sin(x); }; } Test.prototype.b = function (x) { return Math.sin(x); }; return Test; })(); but, I am bit confused about the spec 4.9.2 Arrow Function Expressions