Is there a way to make an “Object.frozen” object throw warnings when an attempt is made to change it?

前端 未结 1 1127
再見小時候
再見小時候 2020-12-17 10:32

I use Object.freeze as a means to prevent myself from breaking my own rules. I would like Object.freeze to speak to me when I try to make a bad assignment. However, Object.f

相关标签:
1条回答
  • 2020-12-17 11:12

    Code in strict mode will throw a TypeError when trying to assign to an unwritable property (ECMA-262: 11.13.1). But do notice you cannot rely on the error being thrown in browsers that don't fully support ES5 strict mode (such as IE9).

    To make your code run in strict mode, add 'use strict'; at the beginning of the JS file or function containing the code and run it in an environment that implements strict mode (see for example this list: http://caniuse.com/#feat=use-strict).

    0 讨论(0)
提交回复
热议问题