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

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.

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

Object literal function creation [duplicate]

This question already has answers here:
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Methods in ES6 objects: using arrow functions
(6 answers)
Closed 4 years ago.
Can't find this info anywhere on the web. Is there any difference (e.g. in performance) between these two methods created with object literal syntax? Or is the mechanism under the hood exactly the same?
let test = {
foo(){
console.log("foo");
return 1;
},
bar: () => {
console.log("bar");
}
}
console.log(test.foo());
console.log(test.bar());
There shouldn't be any performance difference -- the shorthand function property is just syntactic sugar.
However, there's an operational difference. The shorthand notation is short for the traditional function syntax, not an arrow function. So it's equivalent to:
foo: function() {
console.log("foo");
}
Arrow functions have a number of different behaviors from traditional function expressions. See Arrow function vs function declaration / expressions: Are they equivalent / exchangeable?
Your example functions don't do anything that depends on the differences, but in a real application they might.

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 is exactly the meaning of "=>" 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 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

Categories