简体   繁体   中英

Call function dynamically in Javascript

I want to dynamically call a function from a string like "User.find". A script would call the function find() in the object User if the function exists. Here's what I tried:

 var User = {};
 User.find = function(){
     return 1;
 }

 var input = 'User.find';
 var some_data_array = {name: 'John Doe'};
 var method = input.toString().split('.');
 var nameObj = method[0].substring(0,1).toUpperCase() + method[0].substring(1);
 var methodToCall = method[1];

 nameObj.call(methodToCall, some_data_array);

But it always returns:

 nameObj.call(methodToCall, some_data_array);
 TypeError: Object User has no method 'call'

Any idea? I can't use window since it is a node.js problem, the script is not executed in the browser.

You're completely misunderstanding call() .

call() lets you call a method with a different this .

You want to get as property by name:

object[methodName](arg1, arg, ...);

You actually can achieve this. You first would need to get the scope , where your namespace/function/object is defined.

For example, in your code I would assume its window .

So, a little modification of your code would produce the desired result:

var User = {};
User.find = function(x){
    alert(x);
}

 var input = 'User.find';
 var some_data_array = {name: 'John Doe'};
 var method = input.toString().split('.');
 var nameObj = global[method[0]];
 var methodToCall = method[1];

 nameObj[methodToCall](some_data_array.name);

Mark the use of global[] . This is where it starts.

[edited] * Modified code to use global instead of window as being used in nodejs .

What you want to do is access the global object.

This should work:

var User = {};
User.find = function(){
    return 1;
}

var input = 'User.find';
var some_data_array = {name: 'John Doe'};
var method = input.toString().split('.');
var nameObj = method[0].substring(0,1).toUpperCase() +method[0].substring(1);
var methodToCall = method[1];

"use strict";
var global = (1,eval)("this");
alert(global[nameObj][methodToCall](some_data_array));

There are a couple of ways to get the desired result. In this case it is very simple to just use an anonymous function, as follows, passing in the string parameter contained in objName.name :

 var f = function(oName){ if (oName == 'John Doe') { return 1; } }; var objName = {name: 'John Doe'}; console.log( f( objName.name ) ); 

If the OP prefers to use an object's call method, the following code also will suffice:

 var myObj = { myFunc: function() { if (this.name == 'John Doe') { return 1; } } } var objName = { name: 'John Doe' }; console.log(myObj.myFunc.call(objName)); 

A couple of things to note. myObj calls its method myFunc() passing in the object parameter objName. The purpose of this in the method is to read the property name , possessing that ability because of being bound to the object objName.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM