如何在javascript中调用子类内的父类函数

时间:2016-09-18 04:05:07

标签: javascript

这是我的代码 我有两节课 1.具有一个函数first()的分类帐。 2.没有功能的供应商。

现在我想在供应商类中调用first()函数。

https://gyazo.com/bea7ef497b58d390e279d3e2ea668431

var ledger = function(){};
ledger.prototype = {

    first   : function(){
        alert('first function is being called!');
    }
} // END OF ledger CLASS

var supplier = function(){}
supplier.prototype = {

    first();

} // END OF supplier CLASS

jQuery(function(){

    // INHERITANCE 
    supplier.prototype = create.Object(ledger.prototype);

});

提前致谢

1 个答案:

答案 0 :(得分:2)

var Ledger = function() {};

Ledger.prototype.first = function () {
    console.log("First!");
};

var Supplier = function () {
    Ledger.call(this);
};

Supplier.prototype = Object.create(Ledger.prototype);
Supplier.prototype.constructor = Supplier;

Supplier.prototype.second = function () {
  console.log("Second!");
  this.first();
};

var supplier = new Supplier();

// First!
supplier.first();

// Second!
// First!
supplier.second();

console.log(supplier instanceof Ledger);   // true 
console.log(supplier instanceof Supplier); // true