我已经喜欢JavaScript的原型继承
function Vehicle(){
this.isMovable = true;
}
Vehicle.prototype = {
hasTyres:function(){ return true;},
needsFuel:true
};
var Car = function(){
Vehicle.call(this);
this.type = "Car";
};
现在一些代码
它的工作原理,即使我创建原型这样
Car.prototype = Object.create(Vehicle.prototype);
或
Car.prototype = Vehicle.prototype;
是什么区别 ?
我的印象是,
Car.prototype = Object.create(Vehicle);
将使汽车从车辆的继承,但它不是。
谁能解释发生了什么的Object.create方法内
感谢, SRK
如果你想知道什么'Object.create'呢,看看在MDN文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create – 2014-09-28 03:00:28
汽车是一辆汽车,但汽车不一定是汽车(可以是公交车)。所以你不能将它们的原型设置为彼此相等。更多关于这里的原型。 http://stackoverflow.com/a/16063711/1641941 – HMR 2014-09-28 03:30:16