Google App脚本中的子类和继承 [英] Subclasses and Inheritance in Google App Script

查看:89
本文介绍了Google App脚本中的子类和继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以在Google App Script中编写和继承对象的模式? (可用于Google文档的JavaScript)我已经尝试用ParentClass.call(this,args)定义子类,并将父类方法都放在父类的原始定义中,并将它们分配给ParentClass.prototype。但是,虽然此代码通过了单元测试,但它在Google App脚本中使用时失败。解析方案

这是关于类扩展(但javascript doesn '没有真正的'class')。你可以使用 Prototype.js mootool.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 || 无名;
},
getName:function(){
return this.name;
},
getAge:function(){
return this.age;



函数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;


函数Woman(){
this.init.apply(this,arguments);
}
Woman.prototype = {
init:function(){
Human.prototype.init.apply(this,arguments);
this.sex =女人;
},
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}),
女人1 =新女人({年龄:19,姓名:玛丽托马斯));

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());

或者您可以使用jQuery的$ .extend来执行此操作。希望可以帮助!


Anyone have any patterns for writing and subclassing objects in Google App Script? (The JavaScript usable with Google Docs.) I've tried defining subclasses with ParentClass.call(this, args), and putting parent class methods both in the original definition of the parent and assigning them to ParentClass.prototype. But while this code passes unit tests, it fails when used in Google App Script.

解决方案

it's about class extend ( but javascript doesn't have real 'class' ).you may use Prototype.js or mootool.js or do like this?

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() );

or you could use jQuery's $.extend to do this. wish can help !

这篇关于Google App脚本中的子类和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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