Requirejs定义模块
生活随笔
收集整理的這篇文章主要介紹了
Requirejs定义模块
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
模塊可以很好的定義作用域來避免全局名稱空間污染,它可以顯示的定義出函數間的依賴關系,而不需要引用全局變量。
RequireJS可以加載多個不同的模塊,雖然加載的順序不一樣,但是能保證依賴的順序是正確的。
1. ?當模塊中只有鍵值對時,可通過如下方式定義。
| 1 2 3 4 | define({ ????name?:?"beautiful", ????color:?"yellow", ????size:?"3M"}); |
2. 當模塊中沒有依賴,只有函數時,模塊的格式如下
| 1 2 3 4 5 6 7 | define(function()?{ return?{??????????? ????????name?:?"beautiful", ????color:?"yellow", ????size:?"3M" ????} }); |
3. 當模塊中有依賴函數時,先定義出依賴函數的路徑,依賴的函數的別名。
| 1 2 3 4 5 6 7 8 9 10 11 12 | define(["./cart",?"./inventory"],?function(cart,?inventory)?{???? ????return?{???????????????? ????????name?:?"beautiful", ????color:?"yellow", ????size:?"3M" ????addToCart:?function()?{ ????????inventory.decrement(this); ????????cart.add(this); ????} ????????} ????} ); |
| 1 2 3 4 5 6 7 | define(["my/cart",?"my/inventory"],?function(cart,?inventory)?{???????? ????return?function(title)?{???????????? ????????return?title???(window.title?=?title)?: ???????????????????inventory.storeName?+?'?'?+?cart.name; ????????} ????} ); |
本文轉自 genuinecx 51CTO博客,原文鏈接:http://blog.51cto.com/favccxx/1930268,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的Requirejs定义模块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL Server数据库表锁定原理以及
- 下一篇: 谈谈Silverlight 2中的视觉状