How does the new Javascript syntax work regarding functions [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 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.

Related

JavaScript - Unset argument [duplicate]

This question already has answers here:
Are variables statically or dynamically "scoped" in javascript?
(7 answers)
What is the scope of variables in JavaScript?
(27 answers)
What is lexical scope?
(21 answers)
Lexical Scope in JavaScript
(4 answers)
Closed 8 days ago.
I am working on a large library and I want to reduce conflicts in the code as much as possible. I have a function like this:
((privateArgument) => {
var transformed = privateArgument.someProperty;
userCallback(); // This should only have access to transformed
})({someProperty: 'Hello, world!'});
I should mention that I tried to simplify the example as much as possible to illustrate my intention. In reality, I have no control over the value passed to the function ({someProperty: 'Hello, world!'}).
And an example of the userCallback:
() => {
console.log(transformed); // Can be used here freely
console.log(privateArgument); // Can unfortunately also be accessed
}
I have tried the following:
Setting privateArgument = undefined: breaks access to transformed and does not throw the native "undefined" error
Overriding the privateArgument by defining a new let privateArgument and scoping the userCallback with {}: gives me an error, saying that I cannot redefine it
Passing transformed as an argument to userCallback: does nothing to address the issue
Please note that I am specifically asking about arguments, not regular variables such as var and let. I am aware that the issue regarding normal variables has already been addressed in other questions. However, the answers to those questions are not helpful for this particular issue.
If anyone knows of a scoping mechanism I can utilize for this or how to completely get rid of the privateArgument (so that it throws the native error and can be re-declared in the userCallback) it would be greatly appreciated.

Arrow function confusion [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 was doing a question on codesignal and when I couldn't get the answer myself for all tests, revealed the answers and saw this one; while I understand what it's trying to do (make sure positions are not divisible by n, and if it is increment n by one) I'm having trouble understanding the arrow function syntax and, rewriting it myself from their code.
function obst(inputArray) {
for (var n=1;;n+=1) if(inputArray.every(x=>x%n)) return n;}
In Javascript, every function written like this:
function(args) {
// DO SOME STUFF
}
can be written like this:
(args) => {// DO SOME STUFF}
In your case, the method .every() expects a function, and
function(x) {
return x%n;
}
is written as
x => x%n
inputArray.every(x=>x%n)
is the same as
inputArray.every(function (x){
return x%n
}))
( except for the way the 'this' keyword works )
For any doubt about Javascript language, i recommend the You Dont Know Js series
written by Kyle Simpson.
It's a very enlightening book.
Tip: When you write a code, ask question to youself like:
what i'm doing?
assigment means equality?
what are the methods that can i use in string variables?
And something like that.
Stimulate yourself to know what in the actual fudge, you are doing.
Cheers!.

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

Significance of defining custom methods to window object in javascript [duplicate]

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) ?

Use 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.
I recently came across a bit of javascript in my quest to self learn the language which is a clever way of turning the string 'code wars' to C.Wars and 'Barack Hussain obama' to B.H.Obama. My question is on the use of the =>. I could not find details on it. Here is the snippet:
n.split(' ').map((v, i, a) => v.charAt(0).toUpperCase() + (i == a.length - 1 ? v.slice(1) : '.')).join('')
the syntax of map in javascript is:
arr.map(callback[, thisArg])
So would it be right to assume that the => is injecting an anonymous callback function with the parameters v, i and a. From the documentation I see that the callback function for map must contain three arguments:
currentValue
The current element being processed in the array.
index
The index of the current element being processed in the array.
array
The array map was called upon.
Is there any where I can find about the => syntax and its other uses or can someone explain it further to me?
A
That is an arrow function. It represent an anonymous function. It came with the EcmaScript 6 standard.
It does mostly the same as a regular function() {<something>} except it carries on with the "this" from the outer scope. It does not have its own "this". This means you do not need to bind "this" into the callback function anymore.
Here you can find further information.

Categories