Detecting an undefined object property

后端 未结 30 2941
花落未央
花落未央 2020-11-21 04:43

What\'s the best way of checking if an object property in JavaScript is undefined?

30条回答
  •  你的背包
    2020-11-21 05:40

    I would like to show you something I'm using in order to protect the undefined variable:

    Object.defineProperty(window, 'undefined', {});
    

    This forbids anyone to change the window.undefined value therefore destroying the code based on that variable. If using "use strict", anything trying to change its value will end in error, otherwise it would be silently ignored.

提交回复
热议问题