This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Alternatives to eval() for multiple nested objects
I'm trying to find an alternative to eval(). I use it to call a function from a string.
I'm aware of window["myFunction"](args) but my functions are inside another objects.
It can be for example "myObject.anotherOne.myFunction" or "myObject.myFunction" or any kind of deep.
Does someone have an idea please?
Instead of passing a function name and using eval just pass the function directly.
var toCall = function () {
console.log("toCall was called");
};
// Call via eval
var evalSample = function (theName) {
...
eval(theName);
}
evalSample('toCall');
// Call via callback
var funcSample = function (theFunc) {
...
theFunc();
};
funcSample(toCall);
Related
This question already has answers here:
Constructor function vs Factory functions
(8 answers)
Closed 6 years ago.
I'm a total newbie to javascript. Was just wondering what is the difference between the following when using the controllerAs approach, and why the second approach doesn't work:
angular.module("app")
.controller("angularController", angularController);
function angularController() {
this.hello = "hello";
this.goodbye = "goodbye";
}
and
angular.module("app")
.controller("angularController", angularController);
function angularController() {
var hello = "hello";
var goodbye = "goodbye";
return {
hello: hello,
goodbye: goodbye
}
}
The function you register with angular.module.controller is not a factory, it's a constructor.
The second approach is more often used with Angular services.
In the second approach you return tha values in the function call, this is why dont work, the HTML dont call functions, the HTML access propierties.
In the first approach the values are propierties of the controller, this make possible you access the values from the HTML.
Change this in your second approach
this.hello:hello;
this.goodbye:goodbye;
instead of
hello:hello;
goodbye:goodbye;
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:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 7 years ago.
Is there ANY difference in the following two ways of defining a functions?
METHOD 1)
var printName = function(name){
return("Hi! My name is ",name)
}
VS
METHOD 2)
function printName(name){
return("Hi! My name is ",name)
}
and I mean ANY, I'm new at JS and want to lay down my understanding of functions and Objects before I advance as I feel these 2 features are the 2 I'll use the most.
Yes there is a difference, but none that would affect the performance of the function code when it's called.
The difference has to do with when the function is created, but the performance is identical. Using your examples:
printName_1("Drew"); // This will fail, as printName_1 is not defined (yet)
printName_2("user4820485"); // This will work
var printName_1 = function(name){
return "Hi! My name is "+name;
}
function printName_2(name){
return "Hi! My name is "+name;
}
Functions that are declared using the latter syntax are initialized at the beginning of the block where they appear, so it looks like they can be called before they are defined.
This question already has answers here:
Why use named function expressions?
(5 answers)
Closed 8 years ago.
I looked at the jsf.js file of Mojarra 2.2.8 and saw them using the module pattern. Something like this:
name.space = function() {
var utilFunction = function utilFunction() {
// some implementation
};
return {
exposedFunction: function exposedFunction() {
// using utilFunction
}
};
}();
Is there any benefit of giving the functions a name? As opposed to use anonymous functions. They are bound to either a variable or a property of the same name anyway.
Is this some kind of best practice? Does it maybe improve debugging?
I'm just asking, because I usually see the module pattern used with anonymous functions, and was now wondering.
I think it is justified only when using anonymous functions for obvious reading, for example:
async.waterfall[
function makeOne () {},
function makeTwo () {},
];
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Javascript: var functionName = function() {} vs function functionName() {}
I have code with functions defined in two ways:
var retrieveData = function (statusValue, statusText)
{
...
}
function retrieveData(statusValue, statusText) {
..
}
retrieveData(1,2);
Can someone explain what the difference is. Seems that the second way of setting up the function is much simpler.
The 1st example creates a pointer to the function stored in the variable retrieveData, this way you can pass functions like any other variable and retrieve and use them dynamically. Other languages have similar constructs.