你能告诉我如何在javascript中进行封装。我有一个类名称汽车。我想用B类扩展这个类。其次我想覆盖和重载java脚本中的方法。
这是我的小提琴 http://jsfiddle.net/naveennsit/fJGrA/
//Define the Car class
function Car() { }
Car.prototype.speed= 'Car Speed';
Car.prototype.setSpeed = function(speed) {
this.speed = speed;
alert("Car speed changed");
}
//Define the Ferrari class
function Ferrari() { }
Ferrari.prototype = new Car();
// correct the constructor pointer because it points to Car
Ferrari.prototype.constructor = Ferrari;
// replace the setSpeed method
Ferrari.prototype.setSpeed = function(speed) {
this.speed = speed;
alert("Ferrari speed changed");
}
var car = new Ferrari();
car.setSpeed();
你能解释一下这两行吗
Ferrari.prototype = new Car(); 这条线显示法拉利是坐车延伸的?
Ferrari.prototype.constructor = Ferrari;
这条线有什么用?