Is setting properties on the Window object considered bad practice?

后端 未结 3 527
暗喜
暗喜 2020-12-17 10:51

I\'m writing a quite complex JavaScript application that has an MVC architecture that I\'m implementing using Prototype\'s Class support and the Module pattern. The applicat

3条回答
  •  无人及你
    2020-12-17 11:02

    They are useful when you want to call a global function whose name is not known beforehand.

    var funcName = "updateAns" + ansNum;
    window[funcName]();
    

    They can be used to a) avoid evil evals in most cases. b) avoid reference errors to global variables.

    x = x + 1 will generate a reference error if a global x is not defined. window.x = window.x + 1 will not

提交回复
热议问题