Uncaught TypeError: proxy set handler returned false for property '"length"'
Uncaught TypeError: proxy set handler returned false for property '"length"'
我开始测试以下代理模式,并在使用 .splice()
方法时出现标题错误。
class A extends Array {
constructor(...x) {
super(...x)
return new Proxy(this, {
set(o,p,v) {
console.log("set: ",o,p,v)
return o[p] = v
},
get(o,p) {
console.log("get: ",o,p)
return o[p]
},
})
}
}
const a = new A(1,2,3)
a.splice(1,0,"a")
生成以下控制台输出:
get: Array(3) [ 1, 2, 3 ] splice
get: Array(3) [ 1, 2, 3 ] length
get: Array(3) [ 1, 2, 3 ] constructor
set: Array [] length 0
Uncaught TypeError: proxy set handler returned false for property '"length"'
InnerModuleEvaluation self-hosted:2411
InnerModuleEvaluation self-hosted:2411
evaluation self-hosted:2358
有谁知道我缺少哪些内部细节需要 set
到 return true
在使用 .splice()
的情况下?
这不仅仅是 splice
方法,当尝试设置 non-writable 属性 时,所有 strict mode 代码都会发生这种情况。陷阱处理程序的假 return 值表示设置 属性 失败。
"use strict";
const a = new Proxy([1,2,3], {
set(o,p,v) {
console.log("set: ",o,p,v)
return o[p] = v
},
});
a.length = 0; // TypeError: 'set' on proxy: trap returned falsish for property 'length'
在你的情况下,问题是你 return 新的 属性 值,而 a set
trap ought to return a boolean. So it "works" when you set the a.length = 5
, but not for a.length = 0
. Fix this by using the default implementation provided by the Reflect
object 对于陷阱,如果你想要的只是记录而不是实际拦截分配,不要忘记 receiver 参数:
const handler = {
set(o,p,v,r) {
console.log("set: ",o,p,v)
return Reflect.set(o,p,v,r);
},
get(o,p,r) {
console.log("get: ",o,p)
return Reflect.get(o,p,r);
},
}
class A extends Array {
constructor(...x) {
super(...x)
return new Proxy(this, handler)
}
}
我开始测试以下代理模式,并在使用 .splice()
方法时出现标题错误。
class A extends Array {
constructor(...x) {
super(...x)
return new Proxy(this, {
set(o,p,v) {
console.log("set: ",o,p,v)
return o[p] = v
},
get(o,p) {
console.log("get: ",o,p)
return o[p]
},
})
}
}
const a = new A(1,2,3)
a.splice(1,0,"a")
生成以下控制台输出:
get: Array(3) [ 1, 2, 3 ] splice
get: Array(3) [ 1, 2, 3 ] length
get: Array(3) [ 1, 2, 3 ] constructor
set: Array [] length 0
Uncaught TypeError: proxy set handler returned false for property '"length"'
InnerModuleEvaluation self-hosted:2411
InnerModuleEvaluation self-hosted:2411
evaluation self-hosted:2358
有谁知道我缺少哪些内部细节需要 set
到 return true
在使用 .splice()
的情况下?
这不仅仅是 splice
方法,当尝试设置 non-writable 属性 时,所有 strict mode 代码都会发生这种情况。陷阱处理程序的假 return 值表示设置 属性 失败。
"use strict";
const a = new Proxy([1,2,3], {
set(o,p,v) {
console.log("set: ",o,p,v)
return o[p] = v
},
});
a.length = 0; // TypeError: 'set' on proxy: trap returned falsish for property 'length'
在你的情况下,问题是你 return 新的 属性 值,而 a set
trap ought to return a boolean. So it "works" when you set the a.length = 5
, but not for a.length = 0
. Fix this by using the default implementation provided by the Reflect
object 对于陷阱,如果你想要的只是记录而不是实际拦截分配,不要忘记 receiver 参数:
const handler = {
set(o,p,v,r) {
console.log("set: ",o,p,v)
return Reflect.set(o,p,v,r);
},
get(o,p,r) {
console.log("get: ",o,p)
return Reflect.get(o,p,r);
},
}
class A extends Array {
constructor(...x) {
super(...x)
return new Proxy(this, handler)
}
}