相关疑难解决方法(0)

如何在原型上定义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;
}
/* 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.
Run Code Online (Sandbox Code Playgroud)

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

我曾经做过:

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

    get myProperty() {
        // code. …
Run Code Online (Sandbox Code Playgroud)

javascript getter

73
推荐指数
4
解决办法
5万
查看次数

标签 统计

getter ×1

javascript ×1