Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Google应用程序脚本中的子类和继承_Javascript_Google Apps Script - Fatal编程技术网

Javascript Google应用程序脚本中的子类和继承

Javascript Google应用程序脚本中的子类和继承,javascript,google-apps-script,Javascript,Google Apps Script,有人有在谷歌应用程序脚本中编写和子类化对象的模式吗?(JavaScript可用于Google Docs。)我尝试过用ParentClass.call(这是args)定义子类,并将父类方法放在父类的原始定义中,并将它们分配给ParentClass.prototype。但是,虽然这段代码通过了单元测试,但在谷歌应用程序脚本中使用时失败。这是关于类扩展的(但javascript没有真正的“类”)。您可以使用Prototype.js或mootool.js或者这样做 function Human () {

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

这是关于类扩展的(但javascript没有真正的“类”)。您可以使用
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 || "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来实现这一点。希望能帮上忙

这应该可以让你继续:

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

现在更容易了!试试这个

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!`);
  }
}

我对此很失望,但我无法将jQuery引入谷歌应用程序脚本。所以原型不太可能。我认为这没有意义。