Google App脚本中的子类和继承

che*_*vik 5 javascript google-apps-script

任何人都有在Google App Script中编写和子类化对象的模式吗?我试着定义与ParentClass.call子类(这一点,参数),并把父类的方法,无论是在父母的原始定义,并将它们分配给ParentClass.prototype(JavaScript的与谷歌文档可用).但是,虽然此代码通过了单元测试,但在Google App Script中使用时会失败.

小智 3

这是关于类扩展(但javascript没有真正的“类”)。你可以使用Prototype.jsmootool.js或这样做吗?

function Human () {
    this.init.apply ( this, arguments );
}
Human.prototype = {
    init: function () {
        var optns = arguments[0] || {};
        this.age = optns.age || 0;
        this.name = optns.name || "nameless";
    },
    getName : function () {
        return this.name;
    },
    getAge : function () {
        return this.age;
    }
}

function Man () {
    this.init.apply ( this, arguments );
}
Man.prototype = {
    init : function () {
        Human.prototype.init.apply (this, arguments);
        this.sex = "man";
    },
    getName : Human.prototype.getName,
    getAge : Human.prototype.getAge,
    getSex : function () {
        return this.sex;
    }
}
function Woman () {
    this.init.apply ( this, arguments );
}
Woman.prototype = {
    init : function () {
        Human.prototype.init.apply (this, arguments);
        this.sex = "woman";
    },
    getName : Human.prototype.getName,
    getAge : Human.prototype.getAge,
    getSex : Man.prototype.getSex
}

var human = new Human({age:60,name:"Tom Tomas"}),
    man1 = new Man({age:30,name:"Wood Tomas"}),
    woman1 = new Woman({age:19,name:"Mary Tomas"});

console.log( human.getName() );
console.log( man1.getName() );
console.log( woman1.getName() );

console.log( human.getAge() );
console.log( man1.getAge() );
console.log( woman1.getAge() );

console.log( human.getSex && human.getSex() );
console.log( man1.getSex() );
console.log( woman1.getSex() );
Run Code Online (Sandbox Code Playgroud)

或者你可以使用 jQuery 的 $.extend 来做到这一点。希望可以帮忙!