我是Javascript中的noob,我嘗試實現一個模塊,但每當我在這個模塊中調用一個方法時,它將返回undefined。javascript模塊總是返回undefined
請幫忙!
"use strict";
var MODULE = (function() {
var res = {};
res.Student = function(name, firstname, id) {
this.name = name;
this.firstname = firstname;
this.id = id;
this.print = function() {
console.log("student: " + this.name + ', ' + this.firstname + ', ' + this.id);
}
};
res.ForeignStudent = function(name, firstname, id, nationalite) {
Student.apply(this, arguments);
this.nationalite = nationalite;
this.print = function() {
console.log("student: " + this.name + ', ' + this.firstname + ', ' + this.id + ', ' + this.nationalite)
};
};
res.ForeignStudent.prototype = new res.Student();
res.ForeignStudent.prototype.constructor = res.ForeignStudent;
return res;
}());
var x = MODULE;
x.Student("Dupond", "Jean", 1835).print(); // Cannot read property 'print' of undefined
你錯過了'new':'新x.Student( 「杜邦」, 「讓」,1835).PRINT();' –