arrow-functions

Broken autobinding in arrow function for referenced node modules when using react-native with react-relay

被刻印的时光 ゝ 提交于 2019-12-06 03:29:53
I'm using react-native and react-relay, therefore I've the following .babelrc file: { "sourceMaps": "both", "presets": [ "./plugins/babelRelayPlugin", "react-native" ], "passPerPreset": true } Adding a dependency which uses arrow functions in their components as the MKIconToggle from react-native-material-kit ( https://github.com/xinthink/react-native-material-kit ) doesn't get transpiled correctly and the this reference is lost/wrong. The original code which ultimately causes the error looks like the following: _onLayout = (evt) => { this._onLayoutChange(evt.nativeEvent.layout); if (this

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

巧了我就是萌 提交于 2019-12-05 19:12:38
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( {title:'whatever2'}, function( parentFolder ) { resolve(parentFolder); }).then( (parentFolder) => { chrome

How to run ES6 code with arrow functions in Safari?

穿精又带淫゛_ 提交于 2019-12-05 07:46:48
For some reason, ES6 code that runs well in the current Chrome or Firefox cannot run in Safari - for example, arrow functions . As I know, Safari has ok support for ES6. Is there something that needs to be done? Example: var arr = [1,3,5].map((i) => i*i); console.log(arr); Or if it is a full .html file: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <script> "use strict"; var arr = [1,3,5].map((i) => i*i); console.log(arr); </script> </body> </html> Safari (I am using 9.0.3) keeps on giving SyntaxError: Unexpected token '>' Based on the MDN link , (near the

Coffeescript classes and scope and fat and thin arrows

China☆狼群 提交于 2019-12-05 05:34:48
In a fat arrowed function of a coffeescript class, how can I access the scope of the class as well as the function? Example: class Example foo: -> $('.element').each => # or -> @bar($(this)) # I want to access 'bar' as well as the jquery element bar: (element) -> element.hide() So in this example, if I use a => then the @ refers to the this of the class but the 'this' is then wrong, whereas if I use a -> for the each, then the 'this' is correctly scoped but but then how do I reference the class function bar? Thanks! That's because in CoffeeScript @ is an alias for this i.e. when you compile

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

泄露秘密 提交于 2019-12-05 01:25:06
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 className="is-hidden" htmlFor="search">Search</label> <input type="search" onChange={this.onSearchChange}

How to use arrow functions in PHP?

主宰稳场 提交于 2019-12-04 14:29:28
问题 I got to know about arrow functions in PHP 7.4. I tried using them like <?php $num = 1; $arrowfunction = () => { return $num + 1; } echo $arrowfunction(); Because I saw the => operator in the pull request. Just like javascript. I expected '2' as the output but this didn't work! I got Parse error: syntax error, unexpected ')' in /test.php on line 3 回答1: Arrow functions in PHP are introduced in PHP 7.4. They are a little different . The fn keyword The new fn keyword is now a reserved keyword.

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

烂漫一生 提交于 2019-12-04 01:01:39
问题 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

flow generic type for function expression (arrow functions)

左心房为你撑大大i 提交于 2019-12-03 17:31:02
问题 I usually try to keep flow function types separate from their implementation. It's a slightly more readable when I write: type Fn = string => string; const aFn: Fn = name => `hello, ${ name }`; rather than: const aFn = (name: string): string => `hello, ${ name }`; When using generic types we can write: const j= <T>(i: T): T => i; const jString: string = j('apple'); // √ const jNumber: number = j(7); // √ But how can I separate this type from a function expression? type H<T> = (input: T) => T;

ES6 immediately invoke recursive arrow function

萝らか妹 提交于 2019-12-03 12:26:14
This is my current code: const fn = parameter => { // if, else ... fn(X); }; fn(0); Now, I can't use this approach as I need to call the function with a parameter and it must be callable recursively. How to refactor the above arrow function to be immediately invoked and recursively callable? First, let me put the disclaimer that Immediately-Invoked-Function-Expressions (IIFE) are considered bad practice in ES6, and this is tail-recursion and personally I would change it to a for loop. but you can always do this I guess: ((x) =>{ const fn=(p)=>{ //whatever fn(q) } fn(x) })(0) JavaScript

flow generic type for function expression (arrow functions)

放肆的年华 提交于 2019-12-03 06:32:42
I usually try to keep flow function types separate from their implementation. It's a slightly more readable when I write: type Fn = string => string; const aFn: Fn = name => `hello, ${ name }`; rather than: const aFn = (name: string): string => `hello, ${ name }`; When using generic types we can write: const j= <T>(i: T): T => i; const jString: string = j('apple'); // √ const jNumber: number = j(7); // √ But how can I separate this type from a function expression? type H<T> = (input: T) => T; const h:H<*> = i => i; // --> WHAT SHOULD GO FOR '*'? const hString: string = h('apple'); // X error