javascript calling a function on window object

后端 未结 3 1764
清歌不尽
清歌不尽 2021-01-12 22:13

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 wou

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-12 23:02

    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}"]();
    

提交回复
热议问题