我已經喜歡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