我想我不太了解Require.js,所以我想问我为什么可以像下面的例子(全局)那样更改firstName和lastName。Requirejs并避免污染全球范围
不应该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());
});
});
,输出是:
李四
其他一些名其他一些姓氏不在此处polute任何全球范围内
更好地发布你的代码,而不是链接到图像。 – 2013-03-21 17:49:27