我尝试使用地图向数组中的项目添加新字段:
const newArray = oldArray.map(item => {
return (item.newField = 'Something');
});
我已经尝试过:
const newArray = oldArray.map(item => {
item.newField = 'Something';
return item;
});
但是我收到错误:
TypeError: Cannot add property newField, object is not extensible
很可能该对象被标记为不可扩展,并且您正在运行严格模式。
Look at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cant_define_property_object_not_extensible
当该方法Object.preventExtensions(obj)
被调用,你会得到错误。
'use strict';
var obj = {};
Object.preventExtensions(obj);
obj.x = 'foo';
你会得到错误Uncaught TypeError: Cannot add property x, object is not extensible
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)