handler.has()
JavaScript handler.has()方法
用来"隐藏"所需属性的handler.has()方法。这是操作员的陷阱。如果希望访问该属性,它将返回布尔值true;否则,如果原始对象中是否存在键,则返回false。
语法
has: function(target, prop)
参数
target:目标对象。
prop :要检查的属性
返回值
返回布尔值。
浏览器支持
Chrome |
49 |
Edge |
12 |
Firefox |
18 |
Opera |
36 |
示例1
const x = { f:10 };
const proxy = new Proxy(x, {
has: function(target, name) {
document.writeln('in has');
//expected output: in has
return name in target;
}
});
document.writeln('f' in proxy);
//expected output: true
输出:
示例2
var x={hoo:1}
var xyz = new Proxy(x, {
has: function(target, key) {
if(key == "a") return false;
return true;
}
}
)
var x= ("a" in xyz)
var y = ("b" in xyz)
document.write("a in d: " +x)
//expected output: a in d: false
document.write('<br/>');
document.write("b in d: " +y)
//expected output: b in d: true
输出:
示例3
var s={
voo:1
}
var p = new Proxy(s, {
has: function(target, prop) {
document.writeln('called: ' + prop);
return false;
}
});
document.writeln('a' in p);
//expected output:called: a false
输出: