[removed] How to avoid addition of a new property in a function?

后端 未结 3 2011
伪装坚强ぢ
伪装坚强ぢ 2021-01-19 03:43

I am a JS newbie, reading a book Javascript Patterns for understanding. One of the code snippets I could see :

var myFunc = function param() {
...
...
};
myFu         


        
3条回答
  •  别那么骄傲
    2021-01-19 04:31

    JavaScript never really had any options to secure your objects but starting in ECMAScript 5 you can now freeze an object.

    var o = { a: 1, b: 2 };
    Object.freeze(o);
    console.log(o.a); // 1
    o.a = 2;
    console.log(o.a); // 1
    

    There is also a new context known as strict mode where an error can be thrown.

    function processObject(o) {
        "use strict";
        delete o.a; // throws a TypeError
    }
    

提交回复
热议问题