使用bind使用另一个Javascript类';s法

使用bind使用另一个Javascript类';s法,javascript,Javascript,我有一个Javascript中的旧式类,我想在没有完整子类的情况下利用另一个旧式类的行为。大概是这样的: function Foo(x) { this.x = x; } Foo.prototype.add1 = function() { return this.x + 1 } function Bar(n) { this.x = 2*n; } Bar.prototype.add1 = function() { // steal behavior from Foo.add1

我有一个Javascript中的旧式类,我想在没有完整子类的情况下利用另一个旧式类的行为。大概是这样的:

function Foo(x) { 
  this.x = x;
}
Foo.prototype.add1 = function() {
  return this.x + 1
}
function Bar(n) {
  this.x = 2*n;
}
Bar.prototype.add1 = function() {
  // steal behavior from Foo.add1
  return Foo.prototype.add1.call(this);
}
这在浏览器控制台中运行良好:

> b = new Bar(3)
Bar {x: 6}
> b.add1()
7
有没有办法改用
bind
?差不多

function Bar(n) {
  this.x = 2*n;
}
Bar.prototype.add1 = Foo.prototype.add1.bind(xyz);
除了在对象实际存在之前没有“this”对象可用于
xyz

下面的代码可以工作,但似乎是错误的

function Bar(n) {
  this.x = 2*n;
  this.add1 = Foo.prototype.add1.bind(this);
}

在设置旧样式类的原型时,有没有这样使用
bind
的方法?

您不需要使用
bind
,只需指定
Bar.prototype.add1=Foo.prototype.add1
<代码>绑定方法对于实例而不是类很有用。

为什么不正确使用
这个
Bar.prototype.add1=Foo.prototype.add1
?(如果是的话,你能回答吗?)不知何故,我从未想过要这么做。