30分鐘快速入門掌握ES6/ES2015的核心內(nèi)容(下)
前言
在 30分鐘掌握ES6/ES2015核心內(nèi)容(上)我們講解了es6最常用的一些語法:let, const, class, extends, super, arrow functions, template string, destructuring, default, rest arguments
俗話說打鐵要趁熱,今天我們繼續(xù)講es6其他幾個非常有用的新特性。
import export
這兩個家伙對應(yīng)的就是es6自己的module功能。
我們之前寫的Javascript一直都沒有模塊化的體系,無法將一個龐大的js工程拆分成一個個功能相對獨立但相互依賴的小工程,再用一種簡單的方法把這些小工程連接在一起。
這有可能導(dǎo)致兩個問題:
- 一方面js代碼變得很臃腫,難以維護
- 另一方面我們常常得很注意每個script標簽在html中的位置,因為它們通常有依賴關(guān)系,順序錯了可能就會出bug
在es6之前為解決上面提到的問題,我們得利用第三方提供的一些方案,主要有兩種CommonJS(服務(wù)器端)和AMD(瀏覽器端,如require.js)。
如果想了解更多AMD,尤其是require.js,可以參看這個教程:why modules on the web are useful and the mechanisms that can be used on the web today to enable them
而現(xiàn)在我們有了es6的module功能,它實現(xiàn)非常簡單,可以成為服務(wù)器和瀏覽器通用的模塊解決方案。
ES6模塊的設(shè)計思想,是盡量的靜態(tài)化,使得編譯時就能確定模塊的依賴關(guān)系,以及輸入和輸出的變量。CommonJS和AMD模塊,都只能在運行時確定這些東西。
上面的設(shè)計思想看不懂也沒關(guān)系,咱先學(xué)會怎么用,等以后用多了、熟練了再去研究它背后的設(shè)計思想也不遲!好,那我們就上代碼...
傳統(tǒng)的寫法
首先我們回顧下require.js的寫法。假設(shè)我們有兩個js文件: index.js和content.js,現(xiàn)在我們想要在index.js中使用content.js返回的結(jié)果,我們要怎么做呢?
首先定義:
//content.js define('content.js', function(){ return 'A cat'; })
然后require:
//index.js require(['./content.js'], function(animal){ console.log(animal); //A cat })
那CommonJS是怎么寫的呢?
//index.js var animal = require('./content.js') //content.js module.exports = 'A cat'
ES6的寫法
//index.js import animal from './content' //content.js export default 'A cat'
以上我把三者都列出來了,媽媽再也不用擔(dān)心我寫混淆了...
ES6 module的其他高級用法
//content.js export default 'A cat' export function say(){ return 'Hello!' } export const type = 'dog'
上面可以看出,export命令除了輸出變量,還可以輸出函數(shù),甚至是類(react的模塊基本都是輸出類)
//index.js import { say, type } from './content' let says = say() console.log(`The ${type} says ${says}`) //The dog says Hello
這里輸入的時候要注意:大括號里面的變量名,必須與被導(dǎo)入模塊(content.js)對外接口的名稱相同。
如果還希望輸入content.js中輸出的默認值(default), 可以寫在大括號外面。
//index.js import animal, { say, type } from './content' let says = say() console.log(`The ${type} says ${says} to ${animal}`) //The dog says Hello to A cat
修改變量名
此時我們不喜歡type這個變量名,因為它有可能重名,所以我們需要修改一下它的變量名。在es6中可以用as實現(xiàn)一鍵換名。
//index.js import animal, { say, type as animalType } from './content' let says = say() console.log(`The ${animalType} says ${says} to ${animal}`) //The dog says Hello to A cat
模塊的整體加載
除了指定加載某個輸出值,還可以使用整體加載,即用星號(*)指定一個對象,所有輸出值都加載在這個對象上面。
//index.js import animal, * as content from './content' let says = content.say() console.log(`The ${content.type} says ${says} to ${animal}`) //The dog says Hello to A cat
通常星號*結(jié)合as一起使用比較合適。
終極秘籍
考慮下面的場景:上面的content.js一共輸出了三個變量(default, say, type),假如我們的實際項目當中只需要用到type這一個變量,其余兩個我們暫時不需要。我們可以只輸入一個變量:
import { type } from './content'
由于其他兩個變量沒有被使用,我們希望代碼打包的時候也忽略它們,拋棄它們,這樣在大項目中可以顯著減少文件的體積。
ES6幫我們實現(xiàn)了!
不過,目前無論是webpack還是browserify都還不支持這一功能...
如果你現(xiàn)在就想實現(xiàn)這一功能的話,可以嘗試使用rollup.js
他們把這個功能叫做Tree-shaking,哈哈哈,意思就是打包前讓整個文檔樹抖一抖,把那些并未被依賴或使用的東西統(tǒng)統(tǒng)抖落下去。。。
看看他們官方的解釋吧:
Normally if you require a module, you import the whole thing. ES2015 lets you just import the bits you need, without mucking around with custom builds. It's a revolution in how we use libraries in JavaScript, and it's happening right now.
未完待續(xù)
希望更全面了解es6伙伴們可以去看阮一峰所著的電子書ECMAScript 6入門
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
javascript表單驗證 - Parsley.js使用和配置
大家還記得我們曾經(jīng)介紹過的表單驗證jquery插件jquery.validationEngine吧;天介紹的Parsley同樣也可以幫助你只使用簡單的配置即可實現(xiàn)表單驗證功能,基于它的強大DOM-API,感興趣的你可以不要錯過了哦2013-01-01