arrow-functions

Javascript (typescript) Chrome extension, function callback as promises?

巧了我就是萌 提交于 2020-01-13 14:06:54
问题 for a code like this let anotherFolder='whatever'; let anotherFolder2='whatever'; chrome.bookmarks.create( {title:'whatever2'}, function( parentFolder ) { chrome.bookmarks.move( anotherFolder, {parentId: parentFolder.id}, function() { chrome.bookmarks.removeTree( anotherFolder2, function() { resolve(); }); }); }); can I transform it to chain functions? Something like let anotherFolder='whatever'; let anotherFolder2='whatever'; return new Promise(function(resolve){ chrome.bookmarks.create(

Javascript (typescript) Chrome extension, function callback as promises?

这一生的挚爱 提交于 2020-01-13 14:04:06
问题 for a code like this let anotherFolder='whatever'; let anotherFolder2='whatever'; chrome.bookmarks.create( {title:'whatever2'}, function( parentFolder ) { chrome.bookmarks.move( anotherFolder, {parentId: parentFolder.id}, function() { chrome.bookmarks.removeTree( anotherFolder2, function() { resolve(); }); }); }); can I transform it to chain functions? Something like let anotherFolder='whatever'; let anotherFolder2='whatever'; return new Promise(function(resolve){ chrome.bookmarks.create(

Angular2 subscribe understand arrow function

只谈情不闲聊 提交于 2020-01-11 05:09:06
问题 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; } ); 回答1: JS by default executes functions in the scope of the caller. If you pass a function around to be

How to return anonymous object from one liner arrow function in javascript? [duplicate]

旧街凉风 提交于 2020-01-10 20:26:32
问题 This question already has answers here : ECMAScript 6 arrow function that returns an object (6 answers) Closed 3 years ago . I recently switched to es6 and started using arrow functions all over my code. While refactoring I came across below code data.map(function(d) { return {id: d.id, selected: bool}; }); I changed above code to this - data.map((d) => {id: d.id, selected: bool}); But I was getting error from above code. I don't know what is wrong here? I know that If there is no block of

Chrome debugger shows non-obvious (incorrect?) value for this inside an arrow function

偶尔善良 提交于 2020-01-06 08:49:32
问题 Here is an example: function main () { return () => { console.log(42); }; } main()(); If we put a breakpoint on the line with console log statement and inspect the value of this on the Scope panel in DevTools, we can see that: However, if we add this as a second argument to the logging statement, we can see a bit different picture: One always assumes this is borrowed by an arrow function from the outer lexical environment. Since we run the code in the non-strict mode the value for this for

How to get an arrow function's body as a string?

心不动则不痛 提交于 2020-01-05 04:20:10
问题 How to get code as string between {} of arrow function ? var myFn=(arg1,..,argN)=>{ /** *Want to parse * ONLY which is between { and } * of arrow function */ }; If it is easy to parse body of simple function : myFn.toString().match(/function[^{]+\{([\s\S]*)\}$/)[1]; is enough . However, Arrow function does not contains function keyword in its definition . 回答1: This is my attempt: function getArrowFunctionBody(f) { const matches = f.toString().match(/^(?:\s*\(?(?:\s*\w*\s*,?\s*)*\)?\s*?=>\s*){

Best practice in React regarding the context `this`

拟墨画扇 提交于 2020-01-02 07:49:32
问题 Which of the following is the best practice with regard to performance in a React class component: Binding a call back function in the constructor: constructor(props) { /* some code */ this.onChange= this.onChange.bind(this) } render() { return( <div> <input onChange={this.onChange} </div> ); } onChange(event) { /* some code involving 'this' */ } Using an arrow function instead of a normal function: constructor(props) { /* some code */ } render() { return( <div> <input onChange={this.onChange

Converting ECMAScript 6's arrow function to a regular function [duplicate]

时光怂恿深爱的人放手 提交于 2019-12-31 03:10:47
问题 This question already has answers here : What's the meaning of “=>” (an arrow formed from equals & greater than) in JavaScript? (13 answers) Closed 3 years ago . I have the following arrow function if( rowCheckStatuses.reduce((a, b) => a + b, 0) ){} rowCheckStatuses is an array of 1's and 0's, this arrow function adds them all up to produce a number. This number acts as a boolean to determine whether or not there is at least one "1" in the array. The issue is, I don't really understand how

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

主宰稳场 提交于 2019-12-30 07:50:47
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 3 years ago . How to using ES6 Arrow function to realize IIFEImmediately-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 = () =

Immediate function using JavaScript ES6 arrow functions

非 Y 不嫁゛ 提交于 2019-12-28 03:45:11
问题 Does anyone know how to write an immediate function using ES6 arrow syntax? Here's the ES3/5 way of doing it: (function () { //... }()); I've tried the following but get an unexpected token error on the last line. (() => { //... }()); You can test this here: http://www.es6fiddle.net/hsb8bgu4/ 回答1: From the Arrow functions examples, (() => "foobar")() // returns "foobar" So, the function invocation operator should be outside. (() => { //... })(); Sample: http://www.es6fiddle.net/hsb8s1sj/ 回答2: