动态的吸气剂和二传手 - 一种可能性 [英] dynamic getter and setters - a possibility

查看:97
本文介绍了动态的吸气剂和二传手 - 一种可能性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力解决最近出现的问题。
假设我们想要并且知道如何在javascript中使用动态getter和setter,更像是php(__ get,__ set)中的那些。但是由于javascript没有全能属性,我们唯一能做的就是提供一个可能的键列表,并迭代添加getters和setter,并且希望没有其他的东西能来。

I am trying to solve a problem that came to my mind lately. Let's say we would want and would know how to make a point in having dynamic getters and setters in javascript, more like those in php (__get, __set). But as javascript does not have a catch-all property the only thing we could do is to provide a list of possible keys and iterate to add getters and setters on those only, and hope none other will ever come.

但问题到目前为止尚未解决。因此,我想到的下一个方法是使用一个令人讨厌的黑客与尝试 catch ,所以任何时候一个名字在一个对象中是未定义的,使用 catch 作为一个getter(至少),然后恢复代码,很难,也许没有意义。但是从这里出现了我的第二个问题,例如:

But the problem is not by far solved. So the next approach that came to my mind was to use a nasty hack with try and catch, so anytime a name would be undefined in an object to use the catch as a getter (at least) and then resume the code, hard and maybe pointless thing to do. But from here came my second problem, in an use such as this :

console.log(g.someundefinedproperty); 

结果是调用 console.log 显示 undefined ,没有任何异常被抛出。
然后它来找我:如果我使用原来的 window.undefined getter和setter怎么办呢,毕竟每次搞砸时都必须调用它拼错一句话或什么的。

the result would have been a call to console.log showing undefined with no exception to be ever thrown. And then it came to me: what if I would use the original window.undefined getter and setter, after all it must be called every time I screw up and misspell a word or something.

所以我试过

Object.defineProperty(window, 'undefined', {
    get : function ()
    {
         // functional code, including getting the caller and figuring out
         // where we are, and what we have to do... easy :D

         console.log('works');
    },
    set : function ()
    {
         // some couple more fine hacks here
         console.log('this too');
    }
});

但不幸的是,窗口的 undefined 属性是可配置:false
尝试的其他黑客正在克隆窗口对象,除了 undefined 和内部窗口属性。并在新对象上定义新的 undefined (请注意讽刺)然后 window = mybetterwindow ;

But unfortunately the undefined property of window is configurable : false. Other hacks tried were cloning the window object except the undefined and the inner window property. And on the new object to define the new undefined (please take note of the irony) and then window = mybetterwindow;

由于这没有任何问题,我的希望很高,但系统再次失败,因为窗口可以不会被设计覆盖。
我猜测它有它自己的吸气剂,它根据 window.prototype 中的原型甚至更好的窗口重新调整自身。原型(注意大写)。

As this did not rose any issue my hopes were high, but yet again the system failed me as window can't be overwritten, by design. I made a guess it has it's own getter and it's reinstanciate itself based on the prototype found in window.prototype or even better Window.prototype (note the uppercase).

作为本实验的最后一步,我重新定义了 undefined 在这个原型上运行。无济于事,没有任何改变......我尝试创建一个新窗口(),但是 Window 不是构造函数,失败!

As my final step in this experiment I had redefined undefined on this prototype hitted run. To no avail, nothing was changed... I tryed creating a new Window(), but Window is not a constructor, fail!

由于我已经没有想法,我发现自己在这里写这个求助的恳求。
如果您有任何想法如何解决动态getter和setter问题,(生命存在问题,宇宙和其他一切),以某种方式不要以任何方式修改...我使用对象的方式(作为奖励它不需要打破一个在时间和空间的结构中)或语法,我恳请你说话或永远保持沉默:)。

As I have run out of ideas I find myself here writing this pleading for help. If you have any ideas how to solve the dynamic getters and setters problem, (the existencial problem of life, universe and everything else), in a way which does not modify in any way the... way I use the objects (and as bonus it doesn't need to break a hole in the fabric of time and space) or the syntax, I entreat you to speak or forever be silent :).

推荐答案


但不幸的是,窗口的未定义属性是可配置:false

这是真的,因为 EcmaScript 5.1 。以前,它是可以覆盖的。

This is true only since EcmaScript 5.1. Before, it was overwritable.


如果我使用原来的 window.undefined getter and setter,毕竟每当我搞砸了拼错单词或其他东西时都必须调用它。

what if I would use the original window.undefined getter and setter, after all it must be called every time I screw up and misspell a word or something.

不,它会没有工作。 undefined 与全局变量之间存在差异不确定。每次遇到未定义的值时(例如,在 typeof(void 0))中,只有当您明确使用它时,才会评估变量(例如如 g.someprop === undefined )。

No, it would not have worked. There is a difference between the undefined value and the global variable "undefined". The variable is not evaluated each time a undefined value is encountered (e.g. in typeof (void 0)), only when you use it explicitly (such as in g.someprop === undefined).


任何想法如何解决动态getter和setter问题?

Any ideas how to solve the dynamic getters and setters problem?

只有一个解决方案: Proxies 。不幸的是,它只是一个和谐的草案,目前仅在Firefox的Javascript 1.8中支持。 5

There is only one solution: Proxies. Unfortunately, it is only a harmony draft and currently only supported in Firefox' Javascript 1.8.5.

另见是否有相当于__noSuchMethod__特性的属性,或者在JS中实现它的方法?检测何时将新属性添加到Javascript对象?如何检测何时将属性添加到JavaScript对象?实现轮询解决方案(通过 setInterval 检查更改)。

See also Is there an equivalent of the __noSuchMethod__ feature for properties, or a way to implement it in JS?, Detect when a new property is added to a Javascript object? or How to detect when a property is added to a JavaScript object? which implements a polling solution (checking for changes via setInterval).

对于一个干净的解决方案,你当前必须使用一个显式的getter函数,你传递属性名称( g.get(someundefinedproperty))。

For a clean solution, you currently have to use an explicit getter function, which you pass the property name (g.get("someundefinedproperty")).

这篇关于动态的吸气剂和二传手 - 一种可能性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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