[英]Why are the object and constructor.prototype set to Base in JavaScript?
Here is my little program. 这是我的小程序。 When I check the value of rec in the debug mode, the object is Base { x=0, y=0, w=10, more...}. 当我在调试模式下检查rec的值时,该对象为Base {x = 0,y = 0,w = 10,more ...}。 Should it be Rectangle? 应该是矩形吗? Also the constructor.prototype is Base. 而且builder.prototype也是Base。 Why not Shape? 为什么不整形?
function Base() {
}
function Shape(x, y) {
this.x = x;
this.y = y;
}
Shape.prototype = new Base();
Shape.prototype.move = function(x, y) {
this.x += x;
this.y += y;
console.log("x = " + this.x + " y = " + this.y);
};
function Rectangle(x, y, w, h) {
Shape.call(this, x, y);
this.w = w;
this.h = h;
}
Rectangle.prototype = new Shape();
Rectangle.prototype.area = function() {
return this.w * this.h;
};
var rec = new Rectangle(0, 0, 10, 10);
console.log(instanceOf(rec, Rectangle));
function instanceOf(object, constructor) {
while (object != null) {
if (object == constructor.prototype)
return true;
if ( typeof object == 'xml') {
return constructor.prototype == XML.prototype;
}
object = object.__proto__;
}
return false;
}
Have a look at Why [not] to use the new
keyword here? 看看为什么[不]以使用new
这里的关键字? . 。 You might not use it and create a new instance of it, but rather just inherit from Base.prototype
. 您可能不使用它并创建它的新实例,而只是继承自Base.prototype
。
Also the constructor.prototype is Base. 而且builder.prototype也是Base。 Why not Shape? 为什么不整形?
I'm not sure which constructor
you are referring to here: 我不确定在这里指的是哪个constructor
:
The constructor
property of all your objects is Base
, as all of them inherit this prototype from the Base.prototype
object. 您所有对象的constructor
属性都是Base
,因为它们都从Base.prototype
对象继承了此原型。 You did not overwrite it after setting up the inheritance chains. 建立产业链后没有覆盖它。 It is not really necessary, but good style: Shape.prototype.constructor = Shape
and Rectangle.prototype.constructor = Rectangle
- where those prototype objects are the overwritten ones which inherit from Base
. 确实不是必需的,而是好的样式: Shape.prototype.constructor = Shape
和Rectangle.prototype.constructor = Rectangle
这些原型对象是从Base
继承的覆盖对象。
The constructor
parameter of your instanceOf
function. 您的instanceOf
函数的constructor
参数。 You pass in Rectangle
there, so constructor.prototype
is the prototype object of Rectangle
, which inherits from Base
but is different. 您在此处传递Rectangle
,所以constructor.prototype
是Rectangle
的原型对象,该对象继承自Base
但有所不同。
When I check the value of rec in the debug mode, the object is Base { x=0, y=0, w=10, more...} 当我在调试模式下检查rec的值时,该对象为Base {x = 0,y = 0,w = 10,more ...}
Usually not. 通常不会。 Is Base
something special, eg a host object? Base
是否有特殊之处,例如宿主对象? Your rec
object is an instance of Base
, so it might be displayed differently because of that. 您的rec
对象是Base
一个实例,因此可能因此显示不同。
rec
is just an object which inherits from Rectangle.prototype
which inherits from Shape.prototype
which inherits from Base.prototype
which inherits from… Assuming Base
is the function you defined, from Object.prototype
which inherits from null
. rec
只是一个继承自Rectangle.prototype
的对象,而Rectangle.prototype
继承自Shape.prototype
,而Shape.prototype
继承自Base.prototype
,后者继承自...。假设Base
是您定义的函数,而Object.prototype
继承自null
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.