📅  最后修改于: 2020-10-25 09:14:39             🧑  作者: Mango
handler.getOwnPropertyDescriptor()方法是Object.getOwnPropertyDescriptor()的陷阱。它作为目标对象的自身属性存在,并且目标对象不可扩展。
getOwnPropertyDescriptor: function(target, prop)
目标:目标对象。
属性:应检索其描述的属性名称。
此方法返回一个对象或未定义。
Chrome | 49 |
Edge | 12 |
Firefox | 18 |
Opera | 36 |
const proxy = new Proxy({}, {
getOwnPropertyDescriptor: function(target, name) {
document.writeln('Java Script is a scripting language');
//expected output: Java Script is a scripting language
return Object.getOwnPropertyDescriptor(target, name);
}
});
console.dir(Object.getOwnPropertyDescriptor(proxy, 'foo'));
输出:
Java Script is a scripting language
const r={}
const p = new Proxy(r, {
getOwnPropertyDescriptor: function(target, prop) {
document.write('Value : ' + prop);
return { configurable: true, enumerable: true, value: 10 };
}
});
document.writeln(Object.getOwnPropertyDescriptor(p, 'abc ').value);
输出:
Value : abc 10
var a = {
eyeCount: 3
}
var b = {
getOwnPropertyDescriptor(target, prop) {
document.writeln(`Java Script proxt : ${prop}`);
return { configurable: true, value: 50};
}
};
const c= new Proxy(a, b);
document.writeln(Object.getOwnPropertyDescriptor(c, 'Own property').value);
输出:
Java Script proxt : Own property 50