gpt4 book ai didi

javascript - karma : Attempted to assign to readonly property

转载 作者:行者123 更新时间:2023-12-03 07:18:02 31 4
gpt4 key购买 nike

我扩展全局对象:

Object.defineProperty(Object.prototype,'getRecursive', {writable: false, value: function(keyChain){
var keys = keyChain.split('.');

if(keys.length > 1){
var currentKey = keys[0];
keys.splice(0, 1);

return this.getRecursive.call(this[currentKey], keys.join('.'));
}

return this[keys[0]];
}});

当我运行测试时,我有:TypeError: Attempted to allocate to readonly property. at node_modules/karma-jasmine/lib/adapter.js

当我将writable更改为true时,错误消失了,为什么?

value 上的函数是属性的默认值,而 writable 应该允许/拒绝更改此值,我错了吗?

最佳答案

Object.defineProperty() 的文档中所述writable 定义定义的属性是否可以使用赋值运算符进行修改。

When the writable property attribute is set to false, the property is said to be “non-writable”. It cannot be reassigned.

在严格模式下,这将导致您提到的抛出 TypeError 。我不确定在 node_modules/karma-jasmine/lib/adapter.js 的哪一行抛出错误,但您可能会发现那里不允许重新分配,因为 getRecursive 属性被标记为不可写。

关于javascript - karma : Attempted to assign to readonly property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36331285/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com