创建对象后如何在变量上使用 get 运算符?

发布于 2024-10-15 17:05:18 字数 848 浏览 1 评论 0 原文

我最近在此处问了一个问题,我发现我可以使用{get a() {return 'whatev'}} 控制获取和设置变量时发生的情况。

我想知道在创建变量后是否/如何可以做到这一点,就像这样,除了这不起作用:

abc=new Object();
abc._a = NaN;
abc.get a() {
return abc._a;
}
abc.set a(i) {
abc._a=i;
}

在另一个问题中,它只向我展示了在创建新对象时如何做到这一点:

abc = {
  _a:NaN;
  get a() {
    return _a;    
    }
  set a(i) {
    _a=i;
    }
  }

编辑: 我在 https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/defineGetter,它可以工作,但它说它是非标准的。有没有更标准的方法?

I recently asked a question here where I found I could use {get a() {return 'whatev'}} to control what happens when variables are get and set.

I want to know if/how I can do that after a variable is created, something like this, except that this doesn't work:

abc=new Object();
abc._a = NaN;
abc.get a() {
return abc._a;
}
abc.set a(i) {
abc._a=i;
}

In the other question it showed me only how to do it when making a new object:

abc = {
  _a:NaN;
  get a() {
    return _a;    
    }
  set a(i) {
    _a=i;
    }
  }

EDIT:
I found 'defineGetter' and 'defineSetter' on https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/defineGetter, it works, but it says it is non-standard. Is there a more standard way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

奢欲 2024-10-22 17:05:18

getset 是对象初始值设定项的一部分。它们不是对象的属性,因此以后无法添加。

有关详细信息,请参阅 ECMAScript 1.5,第 11.1.5 节对象初始化程序

The get and set are part of the Object Initializer. They are not properties of the object, and as such, can not be added later.

See ECMAScript 1.5, Section 11.1.5 Object Initializer for more info.

压抑⊿情绪 2024-10-22 17:05:18

非标准 __defineGetter__ 和大多数浏览器都支持 __defineSetter__ (Mozilla、WebKit 和 Opera,但不包括 IE)。它们会执行您想要的操作,并且是最受支持的选项(无需使用显式的 getX()setX() 方法替换您的属性)。示例:

var o = {};
o._a = "foo";

o.__defineGetter__("a", function() {
    return this._a;
});

o.__defineSetter__("a", function(val) {
    return this._a = val;
});

alert(o.a);  // "foo"
o.a = "bar";
alert(o._a); // "bar

更新

注意,现在执行此操作的正确方法是使用 Object.defineProperty 或 Object.defineProperties。

The non-standard __defineGetter__ and __defineSetter__ are supported in most browsers (Mozilla, WebKit and Opera but not IE). They will do what you want and are the best-supported option (short of replacing your property with explicit getX() and setX() methods). Example:

var o = {};
o._a = "foo";

o.__defineGetter__("a", function() {
    return this._a;
});

o.__defineSetter__("a", function(val) {
    return this._a = val;
});

alert(o.a);  // "foo"
o.a = "bar";
alert(o._a); // "bar

Update:

Note, the correct way to do this now, is to use Object.defineProperty or Object.defineProperties.

蔚蓝源自深海 2024-10-22 17:05:18

只有 ES5(ECMAScript Edition 5)支持 getter 和 setter。不幸的是,ES5 还没有被任何浏览器完全实现,只是部分实现。

当前的 Javascript 实现不支持 getter 和 setter,不支持标准或跨浏览器方式。抱歉,您必须编写显式的 getA 和 setA 函数。

这是 ES3 中推荐的方法(适用于所有现代浏览器,甚至在 IE6 中),使用闭包:

var abc = (function () {
  var a = NaN;
  return {
    getA: function () {
        return a;
    },
    setA: function (value) {
        a = value;
    }
  };
})();

这将为您提供变量 abc 中的对象,其中包含两个函数:getA() setA(value)。获得 a 的唯一方法是通过这两种方法。

abc.getA()
// NaN

abc.setA(25);
abc.getA();
// 25

a 变量本身对外界是隐藏的,因为它只存在于用于创建对象的匿名函数的范围内。

Only ES5 (ECMAScript Edition 5) will support getters and setters. Unfortunately, ES5 isn’t fully implemented by any browser just yet, only partially.

Current Javascript implementations do not support getters and setters, not in a standard or cross-browser way. You’ll have to write explicit getA and setA functions, sorry.

Here’s the recommended way in ES3 (which works across all modern browsers, even in IE6), using closures:

var abc = (function () {
  var a = NaN;
  return {
    getA: function () {
        return a;
    },
    setA: function (value) {
        a = value;
    }
  };
})();

This will give you an object in the variable abc, with two functions: getA() and setA(value). The only way to get at a is thru those two methods.

abc.getA()
// NaN

abc.setA(25);
abc.getA();
// 25

The a variable itself is hidden from the outside world, because it’s only present within the scope of the anonymous function used to create the object.

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