JavaScript变量在父类中丢失

时间:2015-07-22 10:42:46

标签: javascript

这是父类,Vehicle:

var Vehicle = (function() {
    function Vehicle(year, make, model){
        this.year = year;
        this.make = make;
        this.model = model;
    }

    Vehicle.prototype.getInfo = function(){
        return this.year + " " + this.make + " " + this.model;
    }

    Vehicle.prototype.startEngine = function(){
        return "Vroom!";
    }

    return Vehicle;
});

继承Vehicle的子类Car:

var Car = (function (parent){
    Car.prototype = new Vehicle();
    Car.prototype.constructor = Car;

    function Car(year, make, model){
        parent.call(this, year, make, model);
        this.wheelQuantity = 4;
    }

    Car.prototype.getInfo = function(){
        return "Vehicle Type: Car " + parent.prototype.getInfo.call(this);
    };

    return Car;
})(Vehicle);

当我致电

时,发生了什么
var car  = new Car(2012, 'Toyota', 'Rav 4');

汽车总是以未定义的方式回归。有什么想法为什么?这是代码直接来自Microsoft的HTML5编程和JS和CSS3。

1 个答案:

答案 0 :(得分:3)

您应该在Labeled[ Plot[Sin[2 x], {x, 0, 2 Pi}, AxesLabel -> {Style["\nhr", 20]}, AxesStyle -> Directive[Thick, FontSize -> 20]], {Style["nM", 20, FontFamily -> "Arial", Darker@Gray]}, {{Left, Top}}] 之后添加})();,因为Vehicle变量为Vehicle,如果您没有致电Self-Executing Anonymous Functions

undefined

也在var Vehicle = (function() { function Vehicle(year, make, model){ this.year = year; this.make = make; this.model = model; } Vehicle.prototype.getInfo = function(){ return this.year + " " + this.make + " " + this.model; } Vehicle.prototype.startEngine = function(){ return "Vroom!"; } return Vehicle; })(); var Car = (function (parent){ Car.prototype = new parent(); Car.prototype.constructor = Car; function Car(year, make, model){ parent.call(this, year, make, model); this.wheelQuantity = 4; } Car.prototype.getInfo = function(){ return "Vehicle Type: Car " + parent.prototype.getInfo.call(this); }; return Car; })(Vehicle); var car = new Car(2012, 'Toyota', 'Rav 4'); console.log(car);使用Car而不是全球parent,就像这样

Vehicle