简体   繁体   English

对象文字上的调用函数,以字符串表示-JavaScript

[英]Call function on object literal, represented by string - JavaScript

I am using history.js. 我正在使用history.js。 In the stateObj of the pushState function, I want to add a reference to a function ( Car.init() , or Boat.init() . In C++, I believe I could use a function pointer. 在pushState函数的stateObj中,我想添加对函数的引用( Car.init()Boat.init() 。在C ++中,我相信我可以使用函数指针。

Then on window.onpopstate, I want to reference that function and call it. 然后在window.onpopstate上,我想引用该函数并调用它。 I can read the string ( Car.init() , but then how can I call the function? I don't want to use eval . 我可以读取字符串( Car.init() ,但是如何调用该函数呢?我不想使用eval

You probably shouldn't, but if you do want to invoke a function based on a global dotted-path name, that could be accomplished like this: 您可能不应该这样做,但是如果您确实想基于全局点路径名称调用函数,则可以这样完成:

 function callFunction(name, var_args) { // break the string into individual property/method names var parts = name.split('.'); // start with a reference to the global object var target = window; var previousTarget = null; for (var i = 0; i < parts.length; i++) { // keep a copy of the previous reference to use as the `this` value previousTarget = target; // change the reference to the next named property target = target[parts[i]]; } // grab the remaining arguments var args = Array.prototype.slice.call(arguments, 1); // call the target function, with previousTarget as the subject, using args return target.apply(previousTarget, args); } // This is in the top-level/global scope. This won't work for a local definition. var MyApp = { currentUser: { name: 'Joe', displayName: function(greeting) { alert(greeting + " ," + this.name + "!"); } }, openBar: function() { alert("The Foo Bar is now open for business!"); } }; var functionName = 'MyApp.currentUser.displayName'; callFunction(functionName, "Hello"); 

This is safer than using eval (good call on avoiding it), but is still pretty wacky and very difficult for JavaScript interpreters to optimize. 这比使用eval (避免使用eval更好)更安全,但是仍然很古怪,并且JavaScript解释器很难优化。 Instead, the recommended approach is to use a reference (pointer) to the function. 相反,推荐的方法是使用对该函数的引用(指针)。 This is probably similar to what you'd do in C++. 这可能类似于您在C ++中所做的。 If the function doesn't use this (ie if it's a static function, not a method), you can just take a reference to the function directly. 如果函数不使用this (即,它是静态函数,而不是方法),则可以直接引用该函数。

var open = MyApp.openBar;
open();

If it does have a this value, you'll need to use the .bind() method to preserve its association with the object it's attached to. 如果它确实具有this值,则需要使用.bind()方法来保留其与附加对象的关联。

var display = MyApp.currentUser.displayName.bind(MyApp.currentUser);
display("Greetings");

If you pass additional arguments to .bind() , you can also specify the leading arguments that will be used to call the function. 如果将其他参数传递给.bind() ,则还可以指定将用于调用函数的前导参数。

var displayHello = MyApp.currentUser.displayName.bind(MyApp.currentUser, "Hello");
displayHello();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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