【发布时间】:2015-02-16 10:14:17
【问题描述】:
有没有办法使用代理来检测一个属性是被执行了,还是刚刚被访问?
'use strict';
require('harmony-reflect');
var Stub = {
method: function (a) {
console.log('q' + a + this.q);
}
};
var ProxiedLibrary = {
get: function (target, name, receiver) {
if (name in target) {
return target[name];
}
if (MAGIC_EXPRESSION) {
return function() {
return 'Return from nonexistent function!';
};
}
return 'Property ' + name + ' is drunk and not available at the moment';
}
};
var Library = new Proxy(Stub, ProxiedLibrary);
console.log(Library.nonexistent); //Everything is cool
console.log(Library.nonexistent()); //TypeError we don't want
我非常想模拟 php 的 __call 和 __get,最好是分开。 try...catch 块不是一个选项。
谢谢
【问题讨论】:
标签: javascript ecmascript-harmony