监控所有JavaScript对象属性(魔术getter和setter) [英] Monitor All JavaScript Object Properties (magic getters and setters)

查看:181
本文介绍了监控所有JavaScript对象属性(魔术getter和setter)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在JavaScript中模拟PHP风格的__get()和__set()魔术getter / setter?很多人都说现在这是不可能的。我几乎可以肯定这是可能的,因为像nowjs( http://nowjs.com )这样的项目会做这样的事情。

How do I emulate PHP-style __get() and __set() magic getter/setters in JavaScript? A lot of people say that this is currently impossible. I am almost certain that it is possible because projects like nowjs (http://nowjs.com) do something like this.

我知道你可以利用获取设置 ,但是当你不确定属性名称是什么时,这些都不起作用。例如,如果您希望在创建新属性时执行事件处理程序,该怎么办?

I know that you can utilize get and set, but these don't work when you're not sure what the property name will be. For example, what if you wanted an event handler to execute when a new property is created?

我想要做的示例:

var obj = {};
notify(obj, function(key, value) {
   //key is now the name of the property being set.
   //value is the value of the property about to be set
   console.log("setting " + key + " to " + value);
});
obj.foo = 2; //prints "setting foo to 2"
obj.bar = {a: 2}; //prints "setting bar to [Object]"
//Notice that notify() worked even though 'foo' and 'bar' weren't even defined yet!

(问题类似于以下问题:

(The question is similar to the following questions:

  • Is there a way to monitor changes to an object?
  • JavaScript getter for all properties

编辑:看起来这个功能被称为动态代理,应该出现在ECMAScriptHarmony标准中(可能是ES6)。您可以在这里阅读更多。一个新的'Proxy'对象引入了几个方法(即Create()和createFunction())。

It looks like this feature is called "dynamic proxies" and should appear in the ECMAScript "Harmony" standard (probably ES6). You can read more here. A new 'Proxy' Object is introduced with a couple methods (I.e. Create() and createFunction() ).

可以这样做:

//Constructing an object proxy (proto is optional)
var proxy = Proxy.create(handler, proto);
proxy.foo = 2; //Triggers 'set' function in the handler (read about it)

这里的底线:它没有在大多数浏览器中都可以使用,但Node.js可以使用实现: node-proxy

Bottom line here: it doesn't work in most browsers, but an implementation is available for Node.js: node-proxy.

推荐答案

通过nowjs源代码,我相信他们通过持续监控现在对象并在检测到客户端和服务器之间推送更改。但我承认我还没有完全理解他们的代码。

Looking through the nowjs source code, I believe they do this by continuously monitoring the now object and pushing changes between client and server whenever they are detected. I admit I haven't fully grokked their code yet, however.

在浏览器中,这将通过一些有趣的 setInterval 黑客。

In a browser, this would be done with some fun setInterval hacks.

编辑:是的,这确实是他们所做的:客户端的线368 now.js 。他们这样做一些更多的花样,以便一旦检测到新属性,将来访问它将被getter和setter捕获,但这些修改仅在 setTimeout 中每1000毫秒进行一次。

EDIT: yes, that is indeed what they do: line 368 of the client now.js. They do some more tricks so that once a new property is detected, future access to it is caught by getters and setters, but those modifications are only made every 1000 ms in a setTimeout.

在当前的JavaScript中,这是不可能的另一个证据是 ECMAScript Harmony的代理提议明确地设计用于启用此类方案,这意味着它们目前无法完成。最近的Mozilla浏览器有原型代理实现,如果可能就足够了。显然 V8正在努力增加支持,这可能是这取决于最近使用的V8节点的版本。

Another piece of evidence that this is impossible in current JavaScript is that the proxies proposal for ECMAScript Harmony is designed explicitly to enable such scenarios, implying very strongly that they can't be done currently. Recent Mozilla browsers have a prototype proxies implementation, if perhaps that's enough. And apparently V8 is working to add support, which could be enough depending on what version of V8 Node is using these days.

EDIT2 :哦,很酷,在服务器上显然nowjs确实使用代理!这可能意味着它们在Node中足够成熟以供您使用。请参阅 https://github.com/Flotype/now /blob/master/lib/proxy.js 。或者只是执行 var Proxy = require(nodejs-proxy)并希望他们遵循规范,以便您可以利用MDC和其他地方的文档。

EDIT2: oh cool, on the server side apparently nowjs does use proxies! Which likely means they are mature enough in Node for your usage. See what they do at https://github.com/Flotype/now/blob/master/lib/proxy.js. Or just do var Proxy = require("nodejs-proxy") and hope they follow the spec so you can take advantage of the documentation from MDC and elsewhere.

这篇关于监控所有JavaScript对象属性(魔术getter和setter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆