Tre*_*vor 2 javascript requirejs
我正在重构JavaScript应用程序以使用Require.js.到目前为止,我有这么好的概念性斗争.
在模块中,创建了许多新对象:
new Dealer(self.c, self.config, "Dealership", "2333 South Loop West, Houston, TX 77054", 6.07, "(713) 558-8100", "9am - 9pm", 0, "http://mikecalverttoyota.com/"),
new Dealer(self.c, self.config, "Dealership", "9400 Southwest Freeway, Houston, TX 77074", 8.89, "(713) 270-3900", "8:30am - 9pm", 1, "http://www.sterlingmccalltoyota.com/")
Run Code Online (Sandbox Code Playgroud)
现在,这些创建得很好 - 另一个文件,dealer.js具有对象定义:
function Dealer(c, config, name, address, distance, phone, hours, index, url) {
var self = this;
self.c = c;
self.config = config;
...
}
Run Code Online (Sandbox Code Playgroud)
从概念上讲,如何将Dealer对象转换为require.js模块,以便我可以在其他模块中使用它,然后在这些模块中实例化它的副本?
这是最简单的方法:
define(function () {
function Dealer(c, config, name, address, distance, phone, hours, index, url) {
var self = this;
self.c = c;
self.config = config;
...
}
return Dealer;
});
Run Code Online (Sandbox Code Playgroud)
上面的代码可以在一个名为的文件中dealer.js.然后在您使用它的模块中:
define(['dealer'], function (Dealer) {
new Dealer(self.c, self.config, "Dealership", "2333 South Loop West, Houston, TX 77054", 6.07, "(713) 558-8100", "9am - 9pm", 0, "http://mikecalverttoyota.com/"),
new Dealer(self.c, self.config, "Dealership", "9400 Southwest Freeway, Houston, TX 77074", 8.89, "(713) 270-3900", "8:30am - 9pm", 1, "http://www.sterlingmccalltoyota.com/")
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
803 次 |
| 最近记录: |