Javascript OOP - 继承和原型设计

时间:2013-10-17 15:32:46

标签: javascript oop

我正在尝试在Javascript中使用OOP继承和原型设计。请你看看我的JSfiddel http://jsfiddle.net/Charissima/daaUK/。最后一个值是问题,谢谢。 我无法理解为什么带有raceCar的函数驱动器没有得到totalDistance,每个putTotalDistance设置一个。

        function Car () {
            var that = this;

            this.totalDistance = 0;

            this.putTotalDistance = function(distance) {
                that.totalDistance = distance;
            };

            this.getTotalDistance = function() {
                return this.totalDistance;      
            };  

            this.drive = function(distance) {
                that.totalDistance += distance;     
                return that.totalDistance;
            };

            this.privateFunc = function() { 
                return 'car ' + this.totalDistance;
            };
        };


        function RaceCar (initialDistance) {
            var that = this;

            this.prototype = new Car();
            this.drive = function(distance) {
                return that.prototype.drive(2*distance);
            };

            this.privateFunc = function() {
                return 'raceCar ' + that.getTotalDistance();
            };
        };


        RaceCar.prototype = new Car();

        car = new Car;
        raceCar = new RaceCar;          


        car.putTotalDistance(200);
        alert('car totalDistance = ' + car.drive(10) + ' - ok');

        raceCar.putTotalDistance(200);
        alert('raceCar totalDistance before drive = ' + raceCar.getTotalDistance() + ' - ok');
        alert('raceCar totalDistance after drive = ' + raceCar.drive(10) + ' Why not 220?');                

3 个答案:

答案 0 :(得分:0)

试试这个:

function Car () {    
    this.totalDistance = 0;
};

Car.prototype.putTotalDistance = function(distance) {
    this.totalDistance = distance;
};

Car.prototype.getTotalDistance = function() {
    return this.totalDistance;      
};  

Car.prototype.drive = function(distance) {
    this.totalDistance += distance;     
    return this.totalDistance;
};


function RaceCar () {};
RaceCar.prototype = new Car();
RaceCar.prototype.parent = Car.prototype;
RaceCar.prototype.drive = function(distance) {
    return this.parent.drive.call(this, (distance * 2));
};

raceCar = new RaceCar();
raceCar.putTotalDistance(200);

document.body.innerHTML = 'raceCar totalDistance after drive = ' + raceCar.drive(10);

修改

正如其他一个答案中所指出的,主要问题是在prototype内设置constructor。而是单独设置它。在上面的代码中,我将汽车原型链接到赛车原型父属性,然后使用调用激活父驱动器函数,以便将函数的上下文设置为racecar(通过this),然后传递参数沿。

答案 1 :(得分:0)

谢谢,这很好用,但不幸的是,我需要的另一个功能现在已经坏了。我创建了一个新的JSFiddle:http://jsfiddle.net/Charissima/5g6GV/

car.putTotalDistance(0);            
raceCar.putTotalDistance(100);
var drivingFunctions = [car.drive, raceCar.drive];

myText += drivingFunctions[0](10) + '<br>';
try {
    myText += drivingFunctions[1](100) + '<br>';                
}
catch(err) {
    myText += err + '<br>'
}

答案 2 :(得分:0)

首先var that = this;是不必要的。在对象上下文this将始终引用实例。

您也不希望在它自己的构造函数中设置对象原型。

如果要访问类的原型,请不要尝试通过实例访问它。

The update fiddle

function RaceCar (initialDistance) {
    //var that = this;
    //this.prototype = new Car();

    this.drive = function(distance) {
        return RaceCar.prototype.drive(2*distance);
    };          
};

// This correctly sets the prototype
RaceCar.prototype = new Car();