JavaScript - Call a function by a string [duplicate] - javascript

This question already has answers here:
How to execute a JavaScript function when I have its name as a string
(36 answers)
Closed 8 years ago.
This question is already existing in a different way, but my code is mostly different so I dont get the solution out of the other answers. That's a example Code:
http://jsfiddle.net/e52n28xs/
I want start the function like this:
var test1 = {
start: function() { }
};
var fn = 'test1';
fn.start();
I know the following works, but I need the string option.
test1.start();
Maybe that's impossible?

You could do it with eval() function
var test1 = {
start: function() { }
};
var fn = 'test1';
eval(fn).start()
DEMO

Depending on the value of this in the function you run this, either of these will work:
window['test1'].start();
Or
this['test1'].start();
Obviously, you can do the same with the function name itself, example:
test1['start']();
or
this['test1']['start']();
// same as: var test1 = this['test1']; test1['start']();
If you want the whole thing to be string, then:
eval('test1.start()');
That would also work if you are using var, where in the previous string versions you'd have to have a container object to query.

Related

Create a variable named after a string parameter in JavaScript [duplicate]

This question already has answers here:
Convert string to variable name in JavaScript
(11 answers)
Closed 8 years ago.
I know this piece of code is wrong, but I wonder if there is any way this would be possible.
var createVariableFromParameter = function(myName) {
var myName = [];
return myName;
}
Thank you.
EDIT: I found the solution here: Convert string to variable name in Javascript
I think it's not possible, and even if it could be done, why would you want to do it? There is no meaning in creating variables with random names...
I don't know why you should need it??
But I think true way is :
function create(myvar) { myvar = [];}
var a;
create(a);
function create() { return [];}
var b = new create();

what does this symbol '()' do after closing '{}'in javascript [duplicate]

This question already has answers here:
What is the purpose of a self executing function in javascript?
(21 answers)
Closed 8 years ago.
I'm really sorry with the title, and im sorry if im asking this noob questions here, because i don't know what the keyword for this in google.
Ok, first i'm new to javascript and still learning this programming language. so i've seen this alot, but i don't know what it's mean
var myapp = function(){
var a = 'a';
var b = function(){
//some code goes here
}
return {
init: function(){
b();
//some code goes here
}
}
}() <-- what is it?;
So i've been wondering what this symbol '()' do at the end. and why many people writing a function inside a variable?
That means that the function is being exectued right after its declaration
On the other hand, declaring the function this way:
var myapp = function(){
lets you use it as an object..
Then you can use myapp.init() for example

is there a way to execute a function when I have its name in a string [duplicate]

This question already has answers here:
How to execute a JavaScript function when I have its name as a string
(36 answers)
Closed 8 years ago.
Consider I have a name of a function which does not require any argument in a var -
var fn = "foo";
Can I execute it in some or similar like this -
eval(fn);
It does not work. Please suggest.
My definition of function will look like this -
function foo() {
....do something....
}
Please do not use eval.
If the function is in global scope, simply do
var fn = "foo";
window[fn]();
DEMO
try this
eval(fn)();
or this
eval(fn + "()");

jQuery: How does functions works just by dots (.) [duplicate]

This question already has answers here:
How can jQuery do method chaining? [duplicate]
(7 answers)
Closed 10 years ago.
Since I learned jQuery I've always wondering how does jQuery execute a function after another just by adding dots . (dont know it real name, sorry for that);
$("#somediv").fadeIn("fast").css("background","blue");
When fade effect finished then the CSS function execute. Its like if you can execute whatever function you want one after another.
How can I do that?
Note: If I named something wrong, please correct me, I just want to learn.
It returns the same type of object, here's a really trivial example that demonstrates the technique:
var Counter = function(){
this.val = 0;
};
Counter.prototype.increment = function(){
++this.val;
return this;
};
Counter.prototype.decrement = function(){
--this.val;
return this;
};
Counter.prototype.log = function(){
console.log(this.val);
return this;
};
var o = new Counter();
o.increment().increment().decrement().log().increment().log();
It is called method chaining, where a method returns the object which it called it.
You can also refer the following post on the subject
How can jQuery do method chaining
how does jquery chaining work?
The short answer is quite straightforward. Each method returns the collection of elements that match the selector passed to it, allowing for that same collection to be passed into the next chained method.
look in source for return this

Alternative to eval() in javascript for a deep function [duplicate]

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

Categories

Resources