What is exactly the meaning of "=>" in javascript? [duplicate] - javascript

This question already has answers here:
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
Closed 7 years ago.
What is exactly the meaning of "=>" in javascript?
it is used here for example.
https://github.com/ebidel/polymer-gmail/blob/master/scripts/app.js

=> called Arrow Function
An arrow function expression (also known as fat arrow function) has a
shorter syntax compared to function expressions and lexically binds
the this value (does not bind its own this, arguments, super, or
new.target). Arrow functions are always anonymous.
For more information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Related

Are these 2 JavaScript statements equivalent? [duplicate]

This question already has answers here:
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
What are the advantages/disadvantages for creating a top level function in ES6 with arrows or without?
(2 answers)
Closed 1 year ago.
These 2 statements seem to do the same thing.
const handleClick = () => alert('foo');
and
function handleClick() {
alert('foo');
}
Are they identical and just syntactically different? The first one looks like a variable declaration, whereas the second one is clearly a function definition.
No, they are not the same thing. The arrow function has some limitations:
Does not have its own bindings to this or super, and should not be
used as methods.
Does not have new.target keyword.
Not suitable for
call, apply and bind methods, which generally rely on establishing a
scope.
Can not be used as constructors.
Can not use yield, within its
body.
ES6 arrow functions provide you with an alternative way to write a shorter syntax compared to the function expression. There is no performance difference.
Are arrow functions faster (more performant, lighter) than ordinary standalone function declaration in v8?

What is this form of code called in javascript [duplicate]

This question already has answers here:
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
Closed 4 years ago.
I'm new to Javascript and I want to know what is the below pattern of code called.
GetDatasetProtocols: workbookId => `${API_QUERY_SVC_URL}dataset/protocols/${workbookId}`
That is an arrow function. The most important difference of an arrow function compared to a "regular" function is that the this keyword within an arrow function des not refer to the object the function is called from, but to the scope where the function is defined.
Arrow functions have been introduced in ES6. You can use them in TypeScript also when you target earlier JS versions. TypeScript will produce code that emulates the scope of this in the arrow function.
Besides arrow functions, you also have template literals here: ${API_QUERY_SVC_URL}dataset/protocols/${workbookId}
Check documentation here

How does the new Javascript syntax work regarding functions [duplicate]

This question already has answers here:
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
Closed 5 years ago.
I'm not sure what this is called in Javascript:
exports.withLocalCallback = () => {
return [1, 2, 3].map(x => x + 1)
}
I haven't seen this before and I can't even think of a way to google it. Can someone explain what's happening here?
It's arrow functions, and they work almost the same as normal js functions, with the difference that 'this' is bound to the scope in which the function is defined. So you don't need to bind functions to access the correct object.
The difference is none if you don't need 'this', except is another syntax, which looks more like functional language functions.

What's the name of the "=>" operator in linq (e.g .Select(x=>x.Prop1) ) [duplicate]

This question already has answers here:
How do I pronounce "=>" as used in lambda expressions in .Net [closed]
(16 answers)
Closed 6 years ago.
What's the name of the "=>" operator (e.g in C# linq .Select(x=>x.Prop1) )
It might not even be an operator.
Does it have a different name in different languages (C# vs JavaScript)
Tagging as operator / C# / javascript - Not sure where this fits in...
In C# it's called a "lambda operator". In JS it's called an "arrow function operator".
References:
https://msdn.microsoft.com/en-nz/library/bb311046.aspx
http://www.ecma-international.org/ecma-262/6.0/#sec-arrow-function-definitions
The => shorthand declaration of a function in javascript.
Example usage would be:
addition:(x,y)=>{
return x+y
}
It's sometimes called a "fat arrow" (or "big boned arrow") but most often called a "lambda"

What is the difference between () => {} and function() {} in react-native javascript? [duplicate]

This question already has answers here:
What are the differences (if any) between ES6 arrow functions and functions bound with Function.prototype.bind?
(3 answers)
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
Closed 6 years ago.
I have seen some functions defined as function(){} and some functions defined as () => {}.
Is this related to Javascript version ES6?
Also, how does use of this keyword change from one function definition to another?
The () => {} is called an arrow function. They are, as you said, part of ES6. From the linked page:
An arrow function expression has a shorter syntax compared to function expressions and lexically binds the this value (does not bind its own this, arguments, super, or new.target). Arrow functions are always anonymous.

Categories