是否有相当于__noSuchMethod__属性的属性,或者在JS中实现它的方法? [英] Is there an equivalent of the __noSuchMethod__ feature for properties, or a way to implement it in JS?
问题描述
在某些javascript实现中有一个 noSuchMethod 功能(Rhino,SpiderMonkey)
There is a noSuchMethod feature in some javascript implementations (Rhino, SpiderMonkey)
proxy = {
__noSuchMethod__: function(methodName, args){
return "The " + methodName + " method isn't implemented yet. HINT: I accept cash and beer bribes" ;
},
realMethod: function(){
return "implemented" ;
}
}
js> proxy.realMethod()
implemented
js> proxy.newIPod()
The newIPod method isn't implemented yet. HINT: I accept cash and beer bribes
js>
我想知道,有没有办法为房产做类似的事情?我想编写可以在属性和方法上调度的代理类。
I was wondering, is there was a way to do something similar for properties? I'd like to write proxy classes that can dispatch on properties as well as methods.
推荐答案
现有的只有一个实际上可以做你想要的那一刻,但遗憾的是没有广泛实施:
There is only one existing thing at the moment that can actually do what you want, but unfortunately is not widely implemented:
- ECMAScript Harmony Proxies.
目前只有两个工作实现,在最新的Firefox 4测试版中(自FF3.7预发布以来一直存在)和 node-proxy - Chrome 和 Safari 目前正在研究它。 -
There are only two working implementations available at this time, in the latest Firefox 4 betas (it has been around since FF3.7 pre-releases) and in node-proxy for server-side JavaScript -Chrome and Safari are currently working on it-.
它是下一版本的提案& s =和谐rel =noreferrer>早期提案 ECMAScript ,它是一个API,允许您实现虚拟化对象(代理),您可以在其中分配执行的各种陷阱 - 回调 - 在不同的情况下,你可以完全控制目前的内容 - 在ECMAScript 3/5中只有主机对象可以做到。
It is one of the early proposals for the next version of ECMAScript, it's an API that allows you to implement virtualized objects (proxies), where you can assign a variety of traps -callbacks- that are executed in different situations, you gain full control on what at this time -in ECMAScript 3/5- only host objects could do.
要构建代理对象,你必须使用 Proxy.create
方法,因为您对 set
和 get $感兴趣c $ c>陷阱,我给你一个非常简单的例子:
To build a proxy object, you have to use the Proxy.create
method, since you are interested in the set
and get
traps, I leave you a really simple example:
var p = Proxy.create({
get: function(proxy, name) { // intercepts property access
return 'Hello, '+ name;
},
set: function(proxy, name, value) { // intercepts property assignments
alert(name +'='+ value);
return true;
}
});
alert(p.world); // alerts 'Hello, world'
p.foo = 'bar'; // alerts foo=bar
试一试这里。
代理API是如此新,甚至没有记录在Mozilla开发人员中心,但正如我所说,自Firefox 3.7预发行版以来已经包含了一个有效的实现。
The Proxy API is so new that isn't even documented on the Mozilla Developer Center, but as I said, a working implementation has been included since the Firefox 3.7 pre-releases.
代理
对象在全局范围内可用, create
方法可以使用两个参数,一个处理程序
对象,它只是一个对象它包含名为要实现的陷阱的属性,以及一个可选的 proto
参数,使您能够指定代理继承的对象。
The Proxy
object is available in the global scope and the create
method can take two arguments, a handler
object, which is simply an object that contains properties named as the traps you want to implement, and an optional proto
argument, that makes you able to specify an object that your proxy inherits from.
可用的陷阱是:
// TrapName(args) Triggered by
// Fundamental traps
getOwnPropertyDescriptor(name): // Object.getOwnPropertyDescriptor(proxy, name)
getPropertyDescriptor(name): // Object.getPropertyDescriptor(proxy, name) [currently inexistent in ES5]
defineProperty(name, propertyDescriptor): // Object.defineProperty(proxy,name,pd)
getOwnPropertyNames(): // Object.getOwnPropertyNames(proxy)
getPropertyNames(): // Object.getPropertyNames(proxy)
delete(name): // delete proxy.name
enumerate(): // for (name in proxy)
fix(): // Object.{freeze|seal|preventExtensions}(proxy)
// Derived traps
has(name): // name in proxy
hasOwn(name): // ({}).hasOwnProperty.call(proxy, name)
get(receiver, name): // receiver.name
set(receiver, name, val): // receiver.name = val
keys(): // Object.keys(proxy)
唯一的资源我已经看到,除了提案本身是以下教程:
The only resource I've seen, besides the proposal by itself is the following tutorial:
- Harmony Proxies: Tutorial
编辑:更多信息即将发布,Brendan Eich最近在 JSConf.eu 会议上发表了演讲,你可以在这里找到他的幻灯片:
More information is coming out, Brendan Eich recently gave a talk at the JSConf.eu Conference, you can find his slides here:
- Proxies are Awesome!
这篇关于是否有相当于__noSuchMethod__属性的属性,或者在JS中实现它的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!