Any performance benefit to “locking down” JavaScript objects?

后端 未结 6 1980
Happy的楠姐
Happy的楠姐 2021-02-02 05:18

JavaScript 1.8.5 (ECMAScript 5) adds some interesting methods that prevent future modifications of a passed object, with varying degrees of thoroughness:

<
6条回答
  •  隐瞒了意图╮
    2021-02-02 05:46

    The only reason I see for those methods in production code is, that you can have sealed or frozen objects, for integrity purposes.

    For instance, I write a little library, which works just great and offers you a set of methods in an object, but I don't want to you to change or overwrite any of my properties or methods. I'm not saying I can prevent you from doing that, but I can try to prevent you do it by accident which maybe is more important.

    Also, those methods are easy to 'shim' in environment which doen't know about them, by just returning the original object. Of course it would have no effect then.

    I don't see any performance related reasons to do this.

提交回复
热议问题