2013-05-08 100 views
1

我有一些來自Glen Johnson的書的示例代碼用JavaScript和CSS3編寫的HTML5QUnit測試返回「TypeError:undefined不是函數」

我的 '應用程序代碼' 如下:

// Demo of how implementing inheritance works 

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 Vehicle(); 
    Car.prototype.constructor = Car; 

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

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

    return Car; 
})(Vehicle); 

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

    function Boat(year, make, model) { 
     this.propellerBladeQuality = 3; 
     parent.call(this, year, make, model); 
    } 

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

我的測試如下:

module("Implementing Inheritance", {}); 

test('Creating a Vehicle', function() { 
    expect(2); 
    var actual, 
     expected, 
     v = new Vehicle(2012, 'Toyota', 'Rav4'); 
    actual = v.getInfo(); 
    expected = '2012 Toyota Rav4'; 

    equal(expected, actual); 

    actual = v.startEngine(); 
    expected = 'Vroom'; 
    equal(expected, actual); 
}); 

test('Create a Car', function() { 
    expect(5); 

    var expectedYear = 2012, 
     expectedMake = 'Toyota', 
     expectedModel = 'Rav4', 
     c = new Car(expectedYear, expectedMake, expectedModel); 

    equal(c.year, expectedYear, 'Year'); 
    equal(c.make, expectedMake, 'Make'); 
    equal(c.model, expectedModel, 'Model'); 
    equal(c.wheelQuantity, 4, 'wheelQuality'); 
    equal(c.getInfo(), 'Vehicle Type: Car ' + expectedYear + ' ' + expectedMake + ' ' + expectedModel, 'getInfo()'); 
}); 

test('Create a Boat', function() { 
    expect(5); 

    var expectedYear = 12334, 
     expectedMake = 'dummy-make', 
     expectedModel = 'dummy-model', 
     expectedPropellerBladeQuality = 3, 
     b = new Boat(expectedYear, expectedMake, expectedModel); 

    equal(b.year, expectedYear, 'Year'); 
    equal(b.make, expectedMake, 'Make'); 
    equal(b.model, expectedModel, 'Model'); 
    equal(b.propellerBladeQuality, expectedPropellerBladeQuality, 'propellerBladeQuality'); 
    equal(b.getInfo(), 'Vehicle Type: Boat ' + expectedYear + ' ' + expectedMake + ' ' + expectedModel, 'getInfo()'); 
}); 

當它們運行時,開創汽車測試通過,但是創建小船測試失敗,但有以下例外情況:

Died on test #1  at file:///C://JavaScript_Samples/tests/implementingInheritance.js:33:1: undefined is not a function 
Source:  
TypeError: undefined is not a function 
    at Object.<anonymous> (file:///C:/JavaScript_Samples/tests/implementingInheritance.js:40:13) 

我看不出原因,CarBoat的代碼看起來與我完全相同。

我在Chrome和Firefox上試過這個,結果在兩者中都是一樣的。

有沒有建議嗎?

回答

1

你缺少了創建Boatreturn聲明:

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

    function Boat(year, make, model) { 
     this.propellerBladeQuality = 3; 
     parent.call(this, year, make, model); 
    } 

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

    // insert return statement here 
    return Boat; 
})(Vehicle); 
+0

這就是它!謝謝你,謝謝你,謝謝你! – BanksySan 2013-05-08 23:29:49

相關問題