How does JavaScript “writable” property descriptor work?

和自甴很熟 提交于 2019-12-04 12:36:01

It should be false, not "false". In other words, it should be a boolean.

If you don't pass a boolean, then whatever value you give will be coerced to a boolean, and Boolean("false") === true; // true, so you were effectively passing writable:true.

var TheDarkKnight = Object.create(Superhero, {
    "name": {
        value:"Batman",
        writable:false // boolean false (or any falsey value)
    }
});

TheDarkKnight.name; //"Batman";

TheDarkKnight.name = "Superman";
TheDarkKnight.name; //"Batman";

Also, note that writable:false is the default value, so if you just remove that setting from the descriptor, the property will not be writable.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!