javascript javascript调用window对象上的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10156474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:51:50  来源:igfitidea点击:

javascript calling a function on window object

javascriptjquery

提问by timpone

I have the following code and am wondering how to make the last line work. I addopted a set of api's that current use _view appended as it's namespacing convention and would rather use something like arc.view.$function_name. thx

我有以下代码,想知道如何使最后一行工作。我添加了一组当前使用 _view 附加的 api,因为它是命名空间约定,并且宁愿使用诸如 arc.view.$function_name 之类的东西。谢谢

var arc={};
arc.view={
  say_hello: function(){
    alert("I want to say hello");
  }
}
function say_goodbye(){
  alert("goodbye to you");
}

arc.view.say_hello(); // works
window['say_goodbye'](); // works
// possible to make this work?
window['arc.view.say_hello']();

回答by zetlen

window['arc']['view']['say_hello']();

or

或者

window.arc.view.say_hello()

or

或者

window['arc'].view['say_hello']()

Either the dot syntax or the bracket syntax will work. Dot syntax is really just syntactic sugar for a bracket-based property lookup, so all of the above code snippets are identical. Use bracket syntax when the property name itself is a dynamic value, or when using the property name in dot syntax would cause a syntax error. E.g.:

点语法或括号语法都可以使用。点语法实际上只是基于括号的属性查找的语法糖,因此上述所有代码片段都是相同的。当属性名称本身是动态值时使用括号语法,或者在点语法中使用属性名称会导致语法错误。例如:

var dynamicMethodName = someObject.getMethodName();
someOtherObject[dynamicMethodName]();

or

或者

someOtherObject["a key string with spaces and {special characters}"]();

回答by Gabe

Try this:

试试这个

jsFiddle

js小提琴

window["arc"]["view"]["say_hello"]();

回答by ZER0

Using the square bracket notation you're actually asking to execute a function in window called arc.view.say_hello, and not a function in the object view(that is a property of the object arc). To be more explicit:

使用方括号表示法实际上是要求在调用的窗口中执行函数arc.view.say_hello,而不是对象中的函数view(即对象的属性arc)。更明确地说:

window["arc.view.say_hello"] = function () { alert("hi") };

window["arc.view.say_hello"](); // "hi"

If you want to call a function in the way you described, you have to "resolve" the objects chain. You can create an utility function for that. Something like:

如果你想以你描述的方式调用一个函数,你必须“解析”对象链。您可以为此创建一个实用程序函数。就像是:

var arc={};
arc.view={
  say_hello: function(){
    alert("I want to say hello");
  }
}
function say_goodbye(){
  alert("goodbye to you");
}

function call(id) {
    var objects = id.split(".");
    var obj = this;

    for (var i = 0, len = objects.length; i < len && obj; i++)
        obj = obj[objects[i]];

    if (typeof obj === "function")
        obj();
}

call("say_goodbye");
call("arc.view.say_hello");

You could also extend the utility function to use arguments(or you could just return the reference to the function).

您还可以扩展要使用的实用程序函数arguments(或者您可以只返回对该函数的引用)。