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
Related
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?
This question already has answers here:
What's the difference between a global variable and a 'window.variable' in JavaScript?
(5 answers)
What is the purpose of the var keyword and when should I use it (or omit it)?
(19 answers)
Location of parenthesis for auto-executing anonymous JavaScript functions?
(4 answers)
Closed 5 years ago.
I am working on a project in javascript and while going through design pattern concepts came across following point.
I have a sample code as below:
window.customfun = (function(){}())
Here i have two questions:
1) Can we not simply define a global function as below:
customfun = (function(){}())
2) (function(){}()) is an anonymous function with its own evaluation environment but i have learnt that to be implemented as : (funtion(){})()see the position of parentheses around couple of parentheses. I found that both are working so i wanted to know if there is any difference between them?
Sure, you can define global functions
No, there is actually no difference.
Is there a difference: (1+(1)) === (1) + (1) ?
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.
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.
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