在 Javascript 中获取 Getter 函数

发布于 2024-10-14 19:48:32 字数 516 浏览 4 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故事↓在人 2024-10-21 19:48:32

旧方法

__defineGetter __defineSetter__已被弃用。

访问这些访问器和修改器函数是通过 __lookupGetter__分别是__lookupSetter__

例子

function MyClass() {
 var MyField
 this.__defineGetter__("MyField",function(){
  return MyField
 })
 this.__defineSetter__("MyField",function(value){
  MyField = value
 })
}

var obj = new MyClass()
obj.__lookupSetter__("MyField")("MyValue")
console.log(obj.MyField)

新方法

创建动态访问器或修改器的当前标准方法是使用 Object.definePropertyObject.defineProperties

可以使用 Object.getOwnPropertyDescriptorObject.getOwnPropertyDescriptors

例子

function MyClass() {
 let MyField
 Object.defineProperty(this, 'MyField', {
   get: () => MyField,
   set: value => {
     MyField = value
   }
 })
}

var obj = new MyClass()
Object.getOwnPropertyDescriptor(obj, 'MyField').set('MyValue')
console.log(obj.MyField)

ES6 类

请注意,即使使用 getset 关键字,Object.getOwnPropertyDescriptor 也能工作,但是,如果您使用 <代码>类关键字。

上定义的访问器和修改器不是该类实例的“自己的属性”。相反,它们属于该类的原型(class 只是声明原型的语法糖)。

因为添加到类中的函数会添加到原型中,所以在调用 getter 或 setter 时需要注意上下文。

class MyClass {
  get MyField() {
    return this._myField
  }
  set MyField(value) {
    this._myField = value
  }
}

const obj = new MyClass()
Object.getOwnPropertyDescriptor(MyClass.prototype, 'MyField').set.call(obj, 'MyValue')
console.log(obj.MyField)

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

function MyClass() {
 var MyField
 this.__defineGetter__("MyField",function(){
  return MyField
 })
 this.__defineSetter__("MyField",function(value){
  MyField = value
 })
}

var obj = new MyClass()
obj.__lookupSetter__("MyField")("MyValue")
console.log(obj.MyField)

The new way

The current standard way to create a dynamic accessor or mutator is to use Object.defineProperty or Object.defineProperties.

Accessing accessor and mutator functions can be done using Object.getOwnPropertyDescriptor and Object.getOwnPropertyDescriptors.

Example

function MyClass() {
 let MyField
 Object.defineProperty(this, 'MyField', {
   get: () => MyField,
   set: value => {
     MyField = value
   }
 })
}

var obj = new MyClass()
Object.getOwnPropertyDescriptor(obj, 'MyField').set('MyValue')
console.log(obj.MyField)

ES6 Classes

Note that Object.getOwnPropertyDescriptor works even when using get and set keywords, however there are a couple gotchas if you're using the class 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.

class MyClass {
  get MyField() {
    return this._myField
  }
  set MyField(value) {
    this._myField = value
  }
}

const obj = new MyClass()
Object.getOwnPropertyDescriptor(MyClass.prototype, 'MyField').set.call(obj, 'MyValue')
console.log(obj.MyField)

两相知 2024-10-21 19:48:32

实际上,__lookupGetter____lookupSetter__ 方法已弃用。您必须使用以下命令来代替这些:

/* __lookupGetter__ */
Object.getOwnPropertyDescriptor(obj, 'MyField').get;

/* __lookupSetter__ */
Object.getOwnPropertyDescriptor(obj, 'MyField').set;

Actually, __lookupGetter__ and __lookupSetter__ methods are deprecated. Instead of these you must use:

/* __lookupGetter__ */
Object.getOwnPropertyDescriptor(obj, 'MyField').get;

/* __lookupSetter__ */
Object.getOwnPropertyDescriptor(obj, 'MyField').set;
够钟 2024-10-21 19:48:32

查看lookupGetter

Take a look at lookupGetter.

夜访吸血鬼 2024-10-21 19:48:32

在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文