Difference between window[] and eval() - Javascript

前端 未结 4 2006
滥情空心
滥情空心 2020-12-30 11:56

I\'ve been using both in javascript ... really don\'t know the difference. Googling always shows results for the \"window object\" or \"opening a new window in javascript\"

4条回答
  •  有刺的猬
    2020-12-30 12:31

    eval() interprets arbitrary javascript statements, whereas with window you are accessing a property of the window object.

    In your example, you seem to be using a property name in both eval() and window[]. As the global scope in a browser is the same as the window object's scope they will evaluate to the same thing.

    You can think of your eval("v"+e) statement as being equivalent to eval("window['v'" + e +" ]").

提交回复
热议问题