在 Javascript 中获取 Getter 函数
在 JavaScript 中,可以通过以下方式创建 getter 和 setter:
function MyClass(){
var MyField;
this.__defineGetter__("MyField",function(){
return MyField;
});
this.__defineSetter__("MyField",function(value){
MyField = value;
});
}
但是有没有办法获取 getter 或 setter 函数?我想到了这样的事情:
var obj = new MyClass();
obj.__getSetter__("MyField")("MyValue");
扩展基类时我需要这样的功能。例如: 类“A”具有字段“a”,类“B”从“A”扩展并且也希望具有字段“a”。 要将值从“B”对象的“a”字段传递到“A”对象的“a”字段,我需要在覆盖它们之前获取 setter/getter 函数。
In JavaScript there is the possibility to create getters and setters the following way:
function MyClass(){
var MyField;
this.__defineGetter__("MyField",function(){
return MyField;
});
this.__defineSetter__("MyField",function(value){
MyField = value;
});
}
But is there a way to get the Getter or Setter FUNCTION? I think of something like this:
var obj = new MyClass();
obj.__getSetter__("MyField")("MyValue");
I need such a functionality when extending base classes. For example:
Class "A" has field "a", Class "B" extends from "A" and also wants to have a field "a".
To pass values from the "a"-field of a "B"-object to the "a"-field of a "A"-object I need to get the setter/getter function before overriding them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
旧方法
__defineGetter
和__defineSetter__
已被弃用。
访问这些访问器和修改器函数是通过
__lookupGetter__
和 分别是__lookupSetter__
。例子
新方法
创建动态访问器或修改器的当前标准方法是使用
Object.defineProperty
或Object.defineProperties
。可以使用
Object.getOwnPropertyDescriptor
和Object.getOwnPropertyDescriptors
。例子
ES6 类
请注意,即使使用
get
和set
关键字,Object.getOwnPropertyDescriptor
也能工作,但是,如果您使用 <代码>类关键字。在
类
上定义的访问器和修改器不是该类实例的“自己的属性”。相反,它们属于该类的原型(class
只是声明原型的语法糖)。因为添加到类中的函数会添加到原型中,所以在调用 getter 或 setter 时需要注意上下文。
The old way
__defineGetter
and__defineSetter__
have been deprecated in the time since this question was initially posted.Accessing those accessor and mutator functions was handled with
__lookupGetter__
and__lookupSetter__
respectively.Example
The new way
The current standard way to create a dynamic accessor or mutator is to use
Object.defineProperty
orObject.defineProperties
.Accessing accessor and mutator functions can be done using
Object.getOwnPropertyDescriptor
andObject.getOwnPropertyDescriptors
.Example
ES6 Classes
Note that
Object.getOwnPropertyDescriptor
works even when usingget
andset
keywords, however there are a couple gotchas if you're using theclass
keyword.Accessors and mutators defined on a
class
are not "own properties" of the instances of that class. Instead they belong to the prototype for that class (class
is just syntactic sugar for declaring a prototype).Because the functions added to the class are added to the prototype you will need to be careful about context when calling the getter or setter.
实际上,
__lookupGetter__
和__lookupSetter__
方法已弃用。您必须使用以下命令来代替这些:Actually,
__lookupGetter__
and__lookupSetter__
methods are deprecated. Instead of these you must use:查看lookupGetter。
Take a look at lookupGetter.
在 Google Chrome 中,lookupGetter/lookupSetter 已损坏:http://code.google。 com/p/chromium/issues/detail?id=13175
In Google Chrome lookupGetter/lookupSetter is broken: http://code.google.com/p/chromium/issues/detail?id=13175