Array function of JavaScript && Rest Parameter? [duplicate] - javascript

This question already has answers here:
What does this symbol mean in JavaScript?
(1 answer)
What is the meaning of "...args" (three dots) in a function definition?
(6 answers)
Closed 5 years ago.
I was reading the JavaScript Docs I found the Array function section one Example that I could not understand properly. Because I am learning Programming.
function multiply(multiplier, ...theArgs){
return theArgs.map(x=> multiplier*x);
}
var arr = multiply(2,1,2,3);
console.log(arr);
Above example is giving
2,4,6
My first question why is used 3 dots(...) before function argument name (...theArgs) for? And How did it calculate 2,4,6?
And the second Question is about rest parameter. What does the rest parameter use in Array Function?
I would be glad if anyone can give me some real-life object example to understand this Problem

Related

How is this string as a function argument working (no parentheses?!)? [duplicate]

This question already has answers here:
Backticks (`…`) calling a function in JavaScript
(3 answers)
Closed 1 year ago.
Is this a new way to call a function? Whats this even called? Why do this?
const foo = a => console.log(a)
const k = foo`stuff here?` //whaaaaaaa
//output is ["stuff here?"]
they are called tagged template functions. You can read more on how they work and what they do here: MDN

Method to call a function in javascript in a particular way [duplicate]

This question already has answers here:
How do I write an extension method in JavaScript?
(2 answers)
Add method to string class
(6 answers)
Closed 2 years ago.
I am kinda new to js and would appreciate some help to clarify one subject.
Basically i want to call some functions that i write like default javascript are called:
//declaring function
const splitAsExample = text => text.split('|')
//calling function
splitAsExample('Yesterday|Today|Tomorrow')
Instead of calling the function as mentioned above, i would like to know if it's possible to make a function that can be called like:
'Yesterday|Today|Tomorrow'.splitAsExample()
//and || or
'Yesterday|Today|Tomorrow'.splitAsExample
I learned js all by myself and didn't manage to find a specific name for this question to search up in google. :)
If you can clarify this topic for me it would be great, but if you could give me the name to search it up would be even better!
You could add a prototype function to String.
This allows method chaining with a given object.
String.prototype.splitAsExample = function () { return this.split('|'); };
console.log('Yesterday|Today|Tomorrow'.splitAsExample());

How to make string to object or array format [duplicate]

This question already has answers here:
Access a nested property with a string [duplicate]
(5 answers)
Get global variable dynamically by name string in JavaScript
(6 answers)
Closed 4 years ago.
I have to create a string in format like below
"currentState[0]['children'][1]"
But I need to execute it later just like below
currentState[0]['children'][1]
I have elements and childrens on currentState. But while looping I have to create a string. But later I need to execute as array.
I have tried almost all array methods. Array.call, bind etc. And string methods as well. Could not get the output
How can I make it
Please be more specific with your question but from my understanding, you can use javascript's eval() function to execute a string as javascript, so when you need to execute it, just run eval("currentState[0]['children'][1]").
The alternative to the eval() would be
function evalFn(obj) {
return Function('"use strict";return (' + obj + ')')();
}
evalFn("currentState[0]['children'][1]")
refer to: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval for a more in-depth explanation.

What does '=>' imply in typescript/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.
So I have saw tons of '=>' in some code I found online. Can anyone explain to me like I'm 5?
(I am looking for code, and I will post it here once I find it)..
Got it:
var directive = () =>
{
return new MyDirective();
};
this is ECMASCRIPT 6 standard arrow function.
In this case directive is assigned a function with 0 arguments and one return statement
Details are documented here in MDN Docs
()=> is simply a lambda function, which (in this case) means nothing more than a shorthand notation for a function without a name taking 0 paramaters.
You could have written var directive=function(){return new MyDirective();};
Take a look at John Papas blog Post.

Does JavaScript have a defined order of evaluation for function parameters? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the order of evaluation for function arguments in Javascript?
If I run the following code in Chrome, the parameters to the anonymous function are evaluated left-to-right:
(function () {})(console.log(1), console.log(2));
Is this defined in the specification or is it implementation-defined?
Yes, it is part of the ECMAScript 5 documentation:
Let names be a List containing, in left to right textual order, the Strings corresponding to the identifiers of FormalParameterList. If no parameters are specified, let names be the empty list.
Source: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf (Pg 99)

Categories

Resources