js模組化之自定義模組(頁面模組化載入)
#1 Passing parameter to method
Starting of slow, we let our require module return a JavaScript object with a method that accepts a parameter. This is pretty basic stuff, and should be sufficient for most scenarios.
index.html
<script src="scripts/require.js"></script>
<script>
require(['scripts/app'], function(app){
app.greet('World');
});
</script>
app.js
define(function(){
return {
greet: function(name){
alert('Hello ' + name);
}
};
});
#2 Passing object to method
While #1 works great when you have one, or only a handful of parameters, it can get quite messy when you're dealing with a lot of parameters. To counter this we can let our module accept an object instead.
index.html
<script src="scripts/require.js"></script>
<script>
require(['scripts/app'], function(app){
app.greet({
salutation: 'Dr.',
name: 'Who'
});
});
</script>
app.js
define(function(){
return {
greet: function(config){
alert('Hello ' + config.salutation + ' ' + config.name);
}
};
});
#3 Using RequireJS configuration
By loading the pre-defined RequireJS module aptly named "module" into your own module, you can define and access configuration data.
Note: The definition must be done before you include the require.js file, and this only works for define and not require modules.
index.html
<script>
var require = {
config: {
'app': { //This must match your module name
name: 'Sherlock'
}
}
};
</script>
<script data-main="scripts/app" src="scripts/require.js"></script>
app.js
define(['module'], function(module){
alert('Hello ' + module.config().name);
});
#4 Using a separate configuration module
Sometimes you'll have multiple modules needing the same input, then you can define a separate configuration module, and load it into your module like any other. I think this gives an nice clean separation between logic and data, but it can be a bit of an overkill i many situations.
index.html
<script>
var require = {
config: {
'config': {
name: 'Everybody'
}
}
};
</script>
<script data-main="scripts/app" src="scripts/require.js"></script>
config.js
define(['module'], function(module){
return {
name: module.config().name
}
});
app.js
require(['config'], function(config){
alert('Hello ' + config.name);
});
相關文章
- 前端模組化之迴圈載入前端
- JS模組化JS
- javascript模組化以及載入打包JavaScript
- 明白 JS 模組化JS
- 前端模組化之CommonJS前端JS
- 關於前端模組化 CommonJS、AMD、CMD、ES6中模組載入前端JS
- JavaScript 模組化程式設計之載入器原理JavaScript程式設計
- UEFI載入程式 & 驅動模組化
- node模組載入層級優化優化
- Webpack之模組化優化Web優化
- JS模組化規範JS
- JS模組化系統JS
- ES6 模組化與 CommonJS 模組化區別JS
- Python常用模組(random隨機模組&json序列化模組)Pythonrandom隨機JSON
- python如何匯入自定義模組Python
- ansible自定義模組
- 模組化之AMD、CMD、UMD、commonJSJS
- [譯]JS模組化簡史JS
- 探索 JS 中的模組化JS
- JS模組化程式設計JS程式設計
- anjularjs-filter模組化JSFilter
- 深聊Nodejs模組化NodeJS
- Node.js 的 模組化Node.js
- 【模組化程式設計】理解requireJS-實現一個簡單的模組載入器程式設計UIJS
- 序列化模組,隨機數模組,os模組,sys模組,hashlib模組隨機
- 語義化版本控制模組-Semver
- Node那些事之模組化
- 前端模組化之ES Module前端
- AMD and CMD are dead之js模組化黑魔法JS
- 序列化模組,subprocess模組,re模組,常用正則
- JavaScript模組化JavaScript
- 前端模組化前端
- 解讀js模組化方案modJSJS
- Js模組化開發的理解JS
- Android模組化改造以及模組化通訊框架Android框架
- 模組載入器
- swiper 模組載入
- nodejs模組載入分析(1).mdNodeJS