在 Javascript 中“冻结”数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7509894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:21:53  来源:igfitidea点击:

'Freezing' Arrays in Javascript?

javascriptarraysobjectfreeze

提问by trembl

Since the ECMA-262 specifications Javascript has gained the Object.freeze()method, which allows for objects, whose properties can not be changed, added or removed.

由于 ECMA-262 规范,Javascript 获得了Object.freeze()方法,该方法允许对象的属性无法更改、添加或删除。

var obj = {'a':1, 'b:2'};
Object.freeze(obj);
Object.isFrozen(obj);       // returns true
obj.a = 10;                 // new assignment has no affect
obj.a;                      // returns 1

So far so good.

到现在为止还挺好。

I am wondering, whether freeze() should also work on Arrays.

我想知道,freeze() 是否也适用于数组。

var arr = [1, 2];
Object.freeze(arr);
Object.isFrozen(arr);      // returns true
arr[0] = 10;
arr;                       // returns [10, 2] ... ouch!

Maybe I am wrong, but I was under the impression, that Array inherits from Object.

也许我错了,但我的印象是,Array 继承自 Object。

typeof obj                 // "object"
typeof arr                 // "object"

Any ideas, pointers, enlightenments would be highly appreciated.

任何想法,指针,启示将不胜感激。

回答by CMS

Yes, freeze should work for Arrays, the behavior you are experiencing is clearly an implementation bug.

是的,冻结应该适用于数组,您遇到的行为显然是一个实现错误。

This bug might be related to the fact that array objects implement a custom [[DefineOwnProperty]]internal method (the magic that makes the lengthproperty work).

此错误可能与数组对象实现自定义[[DefineOwnProperty]]内部方法(使length属性起作用的魔法)这一事实有关。

I just tested it on two implementations and it works properly (Chrome 16.0.888, and Firefox Aurora 8.02a).

我刚刚在两个实现上对其进行了测试,它运行正常(Chrome 16.0.888 和 Firefox Aurora 8.02a)。

About your second question, well, array objects inherit from Array.prototypewhich inherits from Object.prototype, for example, you can access non shadowed methods from Object.prototypedirectly on array objects:

关于你的第二个问题,好吧,数组对象继承自Array.prototype继承自Object.prototype,例如,你可以Object.prototype直接从数组对象访问非阴影方法:

['a'].hasOwnProperty('0'); // true

But this isn't related about how the typeofworks, this operator will return 'object'for any object intance, regardless its kind, and for the nullvalue, which people has always complained about.

但这与typeof工作方式无关,该运算符将返回'object'任何对象实例,无论其类型如何,以及null人们一直抱怨的值。

The rest of possible return values of the typeofoperator, correspond to the primitive types of the language, Number, String, Boolean, Symbol and Undefined.

typeof运算符的其余可能返回值,对应于语言的原始类型,Number、String、Boolean、Symbol 和 Undefined。

回答by neemiasjnr

Instead of freeze, use spread operator to copy things without modifying them (if you are using a transpiler, of course):

不要冻结,使用扩展运算符复制内容而不修改它们(当然,如果您使用的是转译器):

const second = {
  ...first,
  test: 20
}