Can I add attributes to 'window' object in javascript?

前端 未结 7 1283
故里飘歌
故里飘歌 2020-12-13 18:02

Can I add any random attribute to \'window\' object in javascript? Some thing like:

window.my_own_attr = \"my_value\"

Does it have any side

7条回答
  •  没有蜡笔的小新
    2020-12-13 18:11

    Yes, you can, but in general you shouldn't.

    The window object is also the JS default "global" object, so all global variables get added there.

    You're unlikely to break anything unless you overwrite a property that's already there, but it's considered bad practise to dump variables on window, or otherwise create lots of global variables.

提交回复
热议问题