Difference between window[] and eval() - Javascript

前端 未结 4 2028
滥情空心
滥情空心 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:43

    Both return a global variable's value. The difference is that if the global variable is undefined, you will get an error on executing eval() whereas window['variableName'] will return undefined(not an error) because accessing an undefined property is not an error but accessing an undefined variable is an error.

提交回复
热议问题