tl;dr:我发现有时在代理的set((陷阱中返回false
不会触发任何TypeError
,有时会触发。为什么会发生这种情况
据我所知,代理中的set((陷阱必须返回true
或false
来表示是否允许写入操作。如果返回了false
,那么应该会触发TypeError
。
然而,这种情况并不总是发生。看看这两个例子:
示例1(无效,但没有TypeError(
let page = { currentPage: "Home" };
page = new Proxy(page, {
set(target, property, value) {
if (typeof value !== "string") return false;
//... additional validation logic could be here
target[property] = value;
return true;
}
});
// Attempt an invalid operation:
page.currentPage = 123;
console.log(page.currentPage); // currentPage is still "Home"
// (writing didn't work, but it didn't trigger any TypeError to let me know!)
示例2(无效,并抛出TypeError((示例取自javascript.info(
let numbers = [];
numbers = new Proxy(numbers, { // (*)
set(target, property, value) { // to intercept property writing
if (typeof val == 'number') {
target[prop] = val;
return true;
} else {
return false;
}
}
});
numbers.push(1); // added successfully
numbers.push(2); // added successfully
console.log("Length is: " + numbers.length); // 2
// Attempt an invalid operation:
numbers.push("test"); // TypeError ('set' on proxy returned false)
alert("This line is never reached (error in the line above)");
无效的集合操作不起作用(正如预期的那样(,但为什么我有时会得到TypeError,有时却不会?理想情况下,我希望总是出现错误,这样我就可以快速发现并纠正我的错误。当我在set((陷阱中返回false时,手动抛出错误的唯一解决方法是什么?
提前非常感谢您的帮助或见解。
set
陷阱返回false
只会在执行赋值的代码以严格模式运行时触发错误。在非严格模式下,false
会阻止分配,但不会出现错误。
在您的第一段代码中,代码仅以非严格模式运行。如果你将其修改为严格的,你会得到一个TypeError:
"use strict";
let page = { currentPage: "Home" };
page = new Proxy(page, {
set(target, property, value) {
if (typeof value !== "string") return false;
//... additional validation logic could be here
target[property] = value;
return true;
}
});
// Attempt an invalid operation:
page.currentPage = 123;
console.log(page.currentPage); // currentPage is still "Home"
// (writing didn't work, but it didn't trigger any TypeError to let me know!)
不同的函数或方法可能运行严格模式或不运行:
let obj = new Proxy(
{ foo: "hello" },
{ set() { return false; } }
);
function nonStrict(x) {
x.foo = 2;
}
function strict(x) {
"use strict";
x.foo = 2;
}
nonStrict(obj); // OK
console.log(obj); // { foo: "hello" }
strict(obj); // Error
console.log(obj);
在第二个代码块中,是.push()
方法进行写入。该方法显然在严格模式下运行,这就是为什么当它试图写入数组索引时会出现TypeError的原因。
所有类(以及其他ES6+结构,如生成器、模块等(都自动处于严格模式:
class Foo {
foo = "hello";
change() {
this.foo = "world";
}
};
const plain = new Foo();
plain.change(); // OK
console.log(plain); // { foo: "world" }
let proxy = new Proxy(
new Foo(),
{ set() { return false; } }
);
proxy.change(); // Error
console.log(proxy);
数组被认为是一个类,因此它的方法是严格的。
const arrayLike = { length: 0 };
Array.prototype.push.call(arrayLike, "hello");
console.log(arrayLike); // { 0: "hello", length: 1 }
let proxy = new Proxy(
arrayLike,
{ set() { return false; } }
);
Array.prototype.push.call(proxy, "world"); // Error
console.log(arrayLike);
您需要使用严格模式从失败的setter中获取异常。在草率模式下,赋值运算符会忽略问题。
(function(){
"use strict";
const page = new Proxy({ currentPage: "Home" }, {
set(target, property, value) {
if (typeof value !== "string") return false;
//... additional validation logic could be here
target[property] = value;
return true;
}
});
page.currentPage = 123; // TypeError, as expected
console.log(page.currentPage);
})();