在 javascript 中通过继承调用两个函数

call two function by Inheritance in javascript

我是 OOPS 的新手 JavaScript。清楚。在这里查看我的 JavaScript 代码,

function a(){
    this.first = "Kar";
}
function b(){
    this.last = "Sho";
}
function c(){
    this.getName = function(){
        return this.first+this.last;
    }
}

c.prototype.u = new a();
c.prototype.v = new b();

var d = new c();

alert(d.getName());

在这里,我得到以下输出,

NaN

但我想打印 KarSho。问题出在哪里?

我知道下面的方法,

b.prototype = new a();
c.prototype = new b();

其实我想要的是,在c中调用ab即可。就是这样。

c 构造函数中同时调用 ab

function a(){
    this.first = "Kar";
}
function b(){
    this.last = "Sho";
}
function c(){
    a.call(this);
    b.call(this);
    this.getName = function(){
        return this.first+this.last;
    }
}

var d = new c();

alert(d.getName());

c.prototype.u = new a();
c.prototype.v = new b();

c.vc.u 原型属性上创建 ab 对象的实例。

要访问它们,您可以通过以下方式调用它们:

function c(){
  this.getName = function(){
    return this.v.first + this.u.last;
  }
}

这不是真正的继承,而是属性的分配。