我想我不太了解 Require.js,所以我想问一下为什么我可以像下面的示例(全局)那样更改 firstName 和 lastName。
Require.js 不应该做一些避免污染全局范围的事情吗?我只是提供用于创建对象的接口,而不是用于更改其内容。非常感谢。
// someModule.js
define([], function() {
function Employee(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
Employee.prototype.getName = function() {
return this.firstName = ' ' = this.lastName;
}
return {
createEmployee: function(fName, lName) {
return new Employee(fName, lName);
};
};
});
// main.js
require(['jquery', 'someModule'], function($, someModule) {
$(function() {
var x = someModule.createEmployee('John', 'Doe');
document.write(x.getName() + '<br>');
x.firstName = 'Some other name';
x.lastName = 'Some other surname';
document.write(x.getName());
});
});
输出是:
John Doe
Some other name Some other surname