似乎我不明白这个constructor概念,所以,我写了一些代码来测试它。假设你有这样的代码:
var test=function(){...}
我知道对象中有一个指向该constructor对象的属性。test.prototypetest
我的问题来了:
这个属性(constructor)是否只属于原型对象?还是所有对象都具有该constructor属性?
我做了另一个测试。如下代码:
function Shape() {
this.x = 0;
this.y = 0;
}
Shape.prototype.move = function(x, y) {
this.x += x;
this.y += y;
console.info("Shape moved.");
};
Rectangle = Object.create(Shape);//inherit from the Shape instead of Shape.prototype
Rectangle.constructor==Function//it is true.
我不知道它是Rectangle.constuctor从哪里来的还是继承自Shape? 谢谢。