arrow-functions

Access variables declared a component from a RxJS subscribe() function

天大地大妈咪最大 提交于 2019-12-18 04:39:11
问题 I am able to use this.variable to access variables in any part of the component, except inside RxJS functions like subscribe() or catch() . In the example below, I want to print a message after running a process: import {Component, View} from 'angular2/core'; @Component({ selector: 'navigator' }) @View({ template: './app.component.html', styles: ['./app.component.css'] }) export class AppComponent { message: string; constructor() { this.message = 'success'; } doSomething() { runTheProcess()

Are ES6 arrow functions incompatible with Angular?

我怕爱的太早我们不能终老 提交于 2019-12-18 04:36:11
问题 Here's a normal ES5 function in my Angular code which works: app.run(function($templateCache){ $templateCache.put('/some','thing') }); I wanted to convert it to ES6 arrow function app.run($templateCache => $templateCache.put('/some','thing')); but it gives the error Uncaught Error: [$injector:unpr] Unknown provider: '/some'Provider <- '/some' http://errors.angularjs.org/1.4.6/$injector/unpr?p0='%2Fsome'Provider%20%3C-%20'%2Fsome' REGEX_STRING_REGEXP @ angular.js:68 (anonymous function) @

Can I use TypeScript overloads when using fat arrow syntax for class methods?

喜夏-厌秋 提交于 2019-12-18 04:29:07
问题 I've converted some classes from the conventional form: class TestOverloads { private status = "blah"; public doStuff(selector: JQuery); public doStuff(selector: string); public doStuff(selector: any) { alert(this.status); } } to use arrow function expressions instead: class TestOverloads2 { private status = "blah"; public doStuff = (selector: any) => { alert(this.status); } } so as to avoid scoping problems when the class methods are used in a callback (see here for background). I can't work

How can I get the smallest two numbers from an array in js?

限于喜欢 提交于 2019-12-17 21:19:56
问题 Hey I've been trying to return the 2 smallest numbers from an array, regardless of the index. Can you please help me out? 回答1: Sort the array in the ascending order. Use Array#slice to get the first two elements (the smallest ones). var arr = [5, 4, 7, 2, 10, 1], res = arr.sort((a,b) => a - b).slice(0, 2); console.log(res); 回答2: While the accepted answer is good and correct, the original array is sorted, which may not be desired var arr = [5, 4, 7, 2, 10, 1], res = arr.sort((a,b) => a - b)

Arrow functions vs Fat arrow functions

狂风中的少年 提交于 2019-12-17 20:17:27
问题 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? 回答1: 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!

Is it possible to export Arrow functions in ES6/7?

泪湿孤枕 提交于 2019-12-17 15:53:10
问题 The export statement below gives a syntax error export default const hello = () => console.log("say hello") why ? I'm only able to export named functions export function hello() { console.log("hello") } What is the reason? 回答1: Is it possible to export Arrow functions in ES6/7? Yes. export doesn't care about the value you want to export. The export statement below gives a syntax error ... why? You cannot have a default export and give it a name ("default" is already the name of the export).

Can I use ES6's arrow function syntax with generators? (arrow notation)

ぐ巨炮叔叔 提交于 2019-12-17 05:40:55
问题 ie how do I express this: function *(next) {} with arrows. I've tried all the combinations I could think of, and I can't find any documentation on it. (currently using node v0.11.14) 回答1: Can I use ES6's arrow function syntax with generators? You can't. Sorry. According to MDN The function* statement ( function keyword followed by an asterisk) defines a generator function. From a spec document (my emphasis): The function syntax is extended to add an optional * token: FunctionDeclaration:

What is the value of “this” inside an arrow function that is defined inside a class [duplicate]

坚强是说给别人听的谎言 提交于 2019-12-13 10:53:23
问题 This question already has answers here : ES6 functions, arrow functions and 'this' in an ES6 class [duplicate] (5 answers) how come a property assigned arrow function binds this at declaration? (2 answers) Difference between adding a method to a class with the arrow symbol and without in ES6? (3 answers) Closed last year . Newbish question but I was wondering what the value of "this" would be for an arrow function that is defined inside a javascript class. Is it a reference to the class

The use of brackets in javascript arrow function declaration

给你一囗甜甜゛ 提交于 2019-12-13 10:23:26
问题 In ES6/ES2015 arrow functions can be declared with or without brackets around the parameter. ie: var foo_1 = myVar => { return myVar + 1; } or: var foo_2 = (myVar) => { return myVar + 2; } What I would like to know is: what is the difference (if any)? 回答1: There is no differences in your example. You need parentheses if you have no parameter: () => ... have multiple parameters: (foo, bar) => ... use destructuring: ({foo}) => ... use default values: (foo = 42) => ... have a rest parameter: (..

Closures and arrow syntax [duplicate]

a 夏天 提交于 2019-12-13 10:17:22
问题 This question already has an answer here : Why doesn't my arrow function return a value? (1 answer) Closed last year . So to my understanding which is obviously wrong at this moment in time is that, return arg => arg*2 is the same as return (arg)=>{arg*2} I always assumed arrow functions are just syntactically neater. But doing this with closures like so doesn't work. function addTwoDigits(firstDigit){ return (secondDigit)=>{firstDigit + secondDigit} } let closure = addTwoDigits(5); console