繁体   English   中英

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

[英]How to call Parent class function inside child class in javascript

这是我的代码,我有两个class 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);

});

提前致谢

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM