想象以下...。
function MyFunctional()
{
this._internal = "";
}
var foo = new MyFunctional();
foo.bar = 0;
foo.gent = "foobar";
每当我致电foo.x = y;
或时如何触发事件foo['x'] = y;
?将创建一个setTimeout并检查属性名称的最后一个数组吗?那似乎有点贵^^
感谢您的阅读,希望得到答案:D
您可以使用proxy
为基本操作定义自定义行为的对象来执行此操作
function MyFunctional() {
this._internal = "";
}
var foo = new MyFunctional();
//define the object whose properties are functions that define the behavior
//of the proxy when an operation is performed on it.
const handler = {
set: function(obj, prop, value) {
if(!(prop in obj)) {
alert(`property '${prop}' added`);
}
obj[prop] = value;
}
}
const p = new Proxy(foo, handler);
p.bar = 0;
p.gent = "foobar";
p.bar = 2;
console.log(foo)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句