Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.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 toString()的继承_Javascript_Oop_Inheritance - Fatal编程技术网

Javascript toString()的继承

Javascript toString()的继承,javascript,oop,inheritance,Javascript,Oop,Inheritance,我有一个方法toString(),它不是从Shape继承的。为什么? function Shape(){ this.name = 'shape'; this.toString = function() {return this.name;}; } function TwoDShape(){ this.name = '2D shape'; } function Triangle(side, height) { this.name = 'Triangle'; this.sid

我有一个方法
toString()
,它不是从
Shape
继承的。为什么?

function Shape(){
  this.name = 'shape';
  this.toString = function() {return this.name;};
}

function TwoDShape(){
  this.name = '2D shape';
}

function Triangle(side, height) {
  this.name = 'Triangle';
  this.side = side;
  this.height = height;
  this.getArea = function(){return this.side * this.height / 2;};
}

TwoDShape.prototype = TwoDShape;
Triangle.prototype = Triangle;

TwoDShape.prototype.constructor = TwoDShape;
Triangle.prototype.constructor = Triangle;

var my = new Triangle(5, 10);

document.write("my getarea: " + my.getArea() + "my name is: " + my.toString()  + "<br>");​
函数形状(){
this.name='shape';
this.toString=function(){返回this.name;};
}
函数TwoDShape(){
this.name='2D shape';
}
功能三角形(侧面、高度){
this.name='Triangle';
这个边=边;
高度=高度;
this.getArea=function(){返回this.side*this.height/2;};
}
TwoDShape.prototype=TwoDShape;
三角形。原型=三角形;
TwoDShape.prototype.constructor=TwoDShape;
Triangle.prototype.constructor=三角形;
var my=新三角形(5,10);
document.write(“my getarea:+my.getarea()+”我的名字是:“+my.toString()+”
”);​

您必须将阴影的原型设置为阴影,而不是像您那样设置为两个图形或三角形。

您必须将阴影的原型设置为阴影,而不是像您那样设置为两个图形或三角形。

三角形的原型必须是一个
形状
,以便继承其方法:

Triangle.prototype = new Shape();
更具体地说,由于您有多个继承级别:

TwoDShape.prototype = new Shape();
TwoDShape.prototype.constructor = TwoDShape;

Triangle.prototype = new TwoDShape();
Triangle.prototype.constructor = Triangle;
也就是说,
TwoDShape
继承
Shape
Triangle
继承自
TwoDShape

一般来说,如果
Foo
继承
Bar
,您将拥有:

Foo.prototype = new Bar(); // inherit Bar
Foo.prototype.constructor = Foo; // Fix constructor which now points to Bar

参考资料:

三角形
的原型必须是
形状
,以便继承其方法:

Triangle.prototype = new Shape();
更具体地说,由于您有多个继承级别:

TwoDShape.prototype = new Shape();
TwoDShape.prototype.constructor = TwoDShape;

Triangle.prototype = new TwoDShape();
Triangle.prototype.constructor = Triangle;
也就是说,
TwoDShape
继承
Shape
Triangle
继承自
TwoDShape

一般来说,如果
Foo
继承
Bar
,您将拥有:

Foo.prototype = new Bar(); // inherit Bar
Foo.prototype.constructor = Foo; // Fix constructor which now points to Bar

参考资料:

原型继承应该是
substructor.Prototype=newparentconstructor
,而不是
substructor.Prototype=ParentConstructor
。您没有正确继承原型,因此没有继承
toString
方法
getArea
不是原型方法,而是每个实例的实际属性,因此它可以工作。

原型继承应该是
SubConstructor.prototype=new ParentConstructor
,而不是
SubConstructor.prototype=ParentConstructor
。您没有正确继承原型,因此没有继承
toString
方法
getArea
不是原型方法,而是每个实例的实际属性,因此它可以工作。

实际上,三角形应该从TwoDShape继承,TwoDShape应该从Shape继承。因此,您需要这样的东西:
TwoDShape.prototype=newshape;Triangle.prototype=新的两个形状在这种情况下,没有明显的区别,因为TwoDShape只更改名称,而Triangle也会覆盖名称。实际上,Triangle应该从TwoDShape继承,TwoDShape应该从Shape继承。因此,您需要这样的东西:
TwoDShape.prototype=newshape;Triangle.prototype=新的两个形状
在本例中,没有明显的区别,因为TwoDShape只更改名称,而Triangle也会覆盖名称。但是,如果您再次需要它,您必须执行类似于
TwoDShape.prototype=新形状的操作示例由Joao Silva给出。但是,如果您再次需要它,您必须执行类似于
TwoDShape.prototype=新形状的操作