如何在原型上定义setter / getter [英] How to define setter/getter on prototype

查看:125
本文介绍了如何在原型上定义setter / getter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2016年10月编辑:请注意这个问题是在2012年提出的。每个月都会有人添加一个新的答案或评论来反驳答案,但这样做并没有多大意义。这个问题可能已经过时了(记住,它是用于 Gnome Javascript 来编写gnome-shell扩展,而不是浏览器的东西,这是非常具体的。)



关于如何在Javascript中进行子类化的我之前的问题,我正在创建一个超类的子类,如下所示:

  function inherits(Child,Parent){
var Tmp = function {};
Tmp.prototype = Parent.prototype;
Child.prototype = new Tmp();
Child.prototype.constructor = Child;
}
/ *定义子类* /
函数Subclass(){
Superclass.apply(this,arguments);
/ *其他初始化* /
}
/ *设置继承* /
inherits(Subclass,Superclass);
/ *添加其他方法* /
Subclass.prototype.method1 = function ... //等等。

我的问题是,如何使用此语法在原型上定义setter / getter ?



我以前做过:

 子类.prototype = {
__proto __:Superclass.prototype,
/ *此处的其他方法... * /

get myProperty(){
//代码。
}
}

但显然以下情况不起作用:

  Subclass.prototype.get myProperty(){/ * code * /} 

我正在使用GJS(GNOME Javascript),并且该引擎与Mozilla Spidermonkey相同或多或少相同。我的代码不适用于浏览器,只要GJS支持它(我猜这意味着Spidermonkey?),我不介意它是否不是交叉兼容的。

  var o = { 
a:7,
得到b(){
返回this.a + 1;
},
设置c(x){
this.a = x / 2
}
};

使用 Object.defineProperty (在支持ES5的现代浏览器上):

  Object.defineProperty(o,myProperty,{
get:function myProperty(){
// code
}
});

或使用 __ defineGetter __ __ defineSetter __ DEPRECATED ):

  var d =日期。原型; 
d .__ defineGetter __(year,function(){return this.getFullYear();});
d .__ defineSetter __(year,function(y){this.setFullYear(y);});


EDIT Oct 2016: Please note this question was asked in 2012. Every month or so someone adds a new answer or comment that refutes an answer, but doesn't really make sense to do so as the question is probably out of date (remember, it was for Gnome Javascript to write gnome-shell extensions, not browser stuff, which is quite specific).

Following my previous question on how to do subclassing in Javascript, I'm making a subclass of a superclass like so:

function inherits(Child,Parent) {
    var Tmp = function {};
    Tmp.prototype = Parent.prototype;
    Child.prototype = new Tmp();
    Child.prototype.constructor = Child;
}
/* Define subclass */
function Subclass() {
    Superclass.apply(this,arguments);
    /* other initialisation */
}
/* Set up inheritance */
inherits(Subclass,Superclass);
/* Add other methods */
Subclass.prototype.method1 = function ... // and so on.

My question is, how do I define a setter/getter on the prototype with this syntax?

I used to do:

Subclass.prototype = {
    __proto__: Superclass.prototype,
    /* other methods here ... */

    get myProperty() {
        // code.
    }
}

But obviously the following won't work:

Subclass.prototype.get myProperty() { /* code */ }

I'm using GJS (GNOME Javascript), and the engine is meant to be the more-or-less same as the Mozilla Spidermonkey one. My code is not intended for a browser so as long as it's supported by GJS (I guess that means Spidermonkey?), I don't mind if it's not cross-compatible.

解决方案

Using an object literal declaration (simplest way):

var o = {
    a: 7,
    get b() {
        return this.a + 1;
    },
    set c(x) {
        this.a = x / 2
    }
};

Using Object.defineProperty (on modern browsers that support ES5):

Object.defineProperty(o, "myProperty", {
    get: function myProperty() {
        // code
    }
});

Or using __defineGetter__ and __defineSetter__ (DEPRECATED):

var d = Date.prototype;
d.__defineGetter__("year", function() { return this.getFullYear(); });
d.__defineSetter__("year", function(y) { this.setFullYear(y); });

这篇关于如何在原型上定义setter / getter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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