gpt4 book ai didi

javascript - QUnit 测试返回 "TypeError: undefined is not a function"

转载 作者:行者123 更新时间:2023-11-28 02:15:36 24 4
gpt4 key购买 nike

我有一些示例代码,来自 Glen Johnson 的书使用 JavaScript 和 CSS3 在 HTML5 中编程

我的“应用代码”如下:

// 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 中尝试过此操作,结果是相同的。

有什么建议吗?

最佳答案

您缺少创建的 Boat 类的 return 语句:

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);

关于javascript - QUnit 测试返回 "TypeError: undefined is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16451843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com