【问题标题】:Subclasses and Inheritance in Google App ScriptGoogle App Script 中的子类和继承
【发布时间】:2012-03-09 00:23:35
【问题描述】:

谁有在 Google App Script 中编写和子类化对象的模式? (可用于 Google Docs 的 JavaScript。)我尝试使用 ParentClass.call(this, args) 定义子类,并将父类方法放在父类的原始定义中并将它们分配给 ParentClass.prototype。但是,虽然这段代码通过了单元测试,但在 Google App Script 中使用时却失败了。

【问题讨论】:

    标签: javascript google-apps-script


    【解决方案1】:

    这是关于类扩展(但 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() );
    

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

    【讨论】:

    • 我对这一切感到失望,但我无法将 jQuery 引入 Google App 脚本。所以原型是非常不可能的。
    【解决方案2】:

    这应该可以帮助您:

    var ApiService = {
    
    /**
     * @params  {string}   bloggerId   The ID of your Blog
     * @returns {array}                Posted entries
     */
      getPosts: function (blogID) {  
        var feed = sendRequest_(feedUrl+blogID +"/posts/default");
        return feed;
      }
    };
    
    
    /**
     * First ParentClass (top level parent)
     */
    ApiService.parent = function (parent) {
        this.parent = parent;
    };
    
    var ParentClass = ApiService.parent.prototype;
    
    /**
     *  Gets the title of a post
     *
     * @param   {object)  post  An XML post entry
     * @returns {string}        The title of the post
     */
    ParentClass.getTitle = function () {
        return this.parent.getElement('title').getText();
    };
    

    你会像这样使用它:

    var firstTitle = ApiService.getPosts()[0].getTitle();     
    

    【讨论】:

    • 我认为这没有意义。
    【解决方案3】:

    现在变得更容易了!试试这个

    function test_inheritance() {
    
      let animal = new Animal("My animal");
      animal.run(22)
    
      let rabbit = new Rabbit("White Rabbit");
    
      rabbit.run(5); // White Rabbit runs with speed 5.
      rabbit.hide(); // White Rabbit hides!
    }
    
    
    class Animal {
      constructor(name) {
        this.speed = 0;
        this.name = name;
      }
      run(speed) {
        this.speed = speed;
        console.log(`${this.name} runs with speed ${this.speed}.`);
      }
      stop() {
        this.speed = 0;
        console.log(`${this.name} stands still.`);
      }
    }
    
    
    class Rabbit extends Animal {
      hide() {
        console.log(`${this.name} hides!`);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多