This question already has answers here:
What is array literal notation in javascript and when should you use it?
(4 answers)
Closed 6 years ago.
im a newbie javascripter, coming from c++/c#.
im learning a tutorial which has the following lines:
function getUserInput(firstName, lastName, callback, callbackObj) {
callback.apply (callbackObj, [firstName, lastName]);
}
this was an example of using a callback function inside another function.
and i was wondering about this line :
callback.apply (callbackObj, [firstName, lastName]);
what is the meaning of using a [ ] inside a function?
This is a literal for an array. In this case it consists of 2 elements, the first being firstName the second lastName.
Since you are using apply, the assumption is that the callback method signature has 2 string parameters: firstName and lastName. The callbackObj is the context being passed to the method (in case it will call this)
More info here
Related
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 3 years ago.
I'm not getting the actual value of my parameter name in the below function. Object.assign taking the function parameter name as a string literal so the resulted JSON object also named as name
see the below code.
Please see the resulted json object i got.
How to fix this?
wrap it with []
return Object.assign(..., {[name]: JSON.parse(...)})
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.
This question already has answers here:
Parameters inside callback function in Javascript
(1 answer)
Higher-Order Functions in JS
(1 answer)
Closed 2 years ago.
I have this code that filters an array using the .filter method.
I'm extremely confused about the function's parameters that is being specified in the .filter method.
Where does the parameter come from? How do I know when to add a parameter like 'value', and what is the value of the parameter 'value'?
var newArray = [1,2,3,4,5,6,7,8,9,10];
newArray = newArray.filter(function(value) {
return value < 6;
});
I'm not too sure if it is the right term to use.
Where does the parameter come from?
It gets passed to the function when the function is called … which will happen somewhere inside filter or a function called by filter.
How do I know when to add a parameter like 'value',
Generally by reading the documentation
what is the value of the parameter 'value'
From the documentation, the first argument is:
element:
The current element being processed in the array.
Array is a JavaScript class and each JavaScript class can have prototype. Filter method is a part of Array prototype and this function is getting as an argument callback function with one argument.
Inside Filter function, your callback is being called on each array item.
You can find more informations about this function in JavaScript docs.
This question already has answers here:
Calling function inside object using bracket notation
(2 answers)
Closed 6 years ago.
I rather have a seemingly trivial issue, but am not able to figure out an efficient approach.
I have a list of about 50 functions to be called such as :
globalClient.funcA(...)
globalClient.funcB(...)
globalClient.funcC(...)
My code should ideally dynamically create the name of the function (funcA / funcB/ funcC and then proceed to actually call that function. My approach below does not work (please note that these aren't exactly the actual names of the functions. I'm only giving these arbitrary names for simplicity of understanding):
var functionName = 'func'.concat('A');
globalClient.functionName
The second line is where it errors out. Now JS thinks that functionName itself is the name of the function. What I want it to do is resolve functionName to funcA and then call globalClient.funcA(...) instead.
I've thought about implementing a switch / case for this but I'm sure there is a far simpler appraoch. Any ideas?
You could use the bracket notation as property accessor.
globalClient[functionName]()
You can use the [ ] operator for accessing the properties.
var globalClient = {
funcA: function(){
console.log('funcA is called');
}
}
var functionName = 'func'.concat('A');
globalClient[functionName]();
This question already has answers here:
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
Closed 6 years ago.
I have found an example of somebody using the .filter() function without passing it a function as a parameter. I am puzzled as to how this works, here is an example of the code:
var integers = [1,2,3,4,5,6,7,8,9,10];
var even = integers.filter(int => int % 2 === 0);
console.log(even); // [2,4,6,8,10]
I am confused because I thought that filter must take a function as an argument, but instead is comparing "int" against "int % 2 === 0".
How is this happening? why does "int" not have to be declared and why can filter accept something that isn't a function?
Thanks!
The parameter of the example IS a function, arrow function:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
It's basically a shorthand syntax for declaring a function which returns the first statement;
That's a function defined using the "fat arrow" notation (=>). It's also called a lambda and is new in ES6.