SlideShare a Scribd company logo
ES2015 –Destructuring & module
IAN
let
let
const
Solv:
Object.freeze
const foo = Object.freeze({});
foo.prop = 123; // 不起作用
Destructuring
 var a = 1;
 var b = 2;
 var c = 3;
 ES6允許寫成
 var [a, b, c] = [1, 2, 3];
 解構不成功,foo的值都會等於undefined。
 不完全解構
 解構賦值不僅適用於var命令,也適用於let和const命令。
 Iterator
 解構失敗,變數的值等於undefined。
函數的參數也可以使用解構
函數參數的解構也可以使用默認值
 (1)交換變數的值
 [x, y] = [y, x];
 (2)從函數返回多個值
 (3)函數參數的定義
 (4)提取JSON數據
 (5)函數參數的默認值
 避免了在函數體內部再寫var foo = config.foo || 'default foo';
 (6)遍歷Map結構
 (7)輸入模塊的指定方法
 const { SourceMapConsumer, SourceNode } = require("source-
map");
Module
 Before ES6
 Require.js 、 CommonJS、AMD.js
 "use strict"
Module - export
 // profile.js
 export var firstName = 'Michael';
 export var lastName = 'Jackson';
 export var year = 1958;
 // profile.js
 var firstName = 'Michael';
 var lastName = 'Jackson';
 var year = 1958;
 export {firstName, lastName, year};
Module - export
 export function multiply (x, y) { return x * y;};
 function v1() { ... }
 function v2() { ... }
 export {
 v1 as streamV1,
 v2 as streamV2,
 v2 as streamLatestVersion
 };
Module - import
 // main.js
 import {firstName, lastName, year} from './profile';
 function setName(element) {
 element.textContent = firstName + ' ' + lastName;
 }
Module - import
 import { lastName as surname } from './profile';
 多重加載
 import { Vehicle } from './Vehicle';
 class Car extends Vehicle {
 move () {
 console.log(this.name + ' is spinning wheels...')
 }
 }
 export { Car }
 export { es6 as default } from './someModule';
 // equals
 import { es6 } from './someModule';
 export default es6;
Module - *
 // circle.js
 export function area(radius) { return Math.PI * radius * radius;}
 export function circumference(radius) { return 2 * Math.PI * radius;}
 // main.js
 import { area, circumference } from './circle';
 console.log("圓面積:" + area(4));
 console.log("圓周長:" + circumference(14));
 import * as circle from './circle';
 console.log("圓面積:" + circle.area(4));
 console.log("圓周長:" + circle.circumference(14));
Module - module
 // main.js
 module circle from './circle';
 console.log("圓面積:" + circle.area(4));
 console.log("圓周長:" + circle.circumference(14));
Module - export default
 // export-default.js
 export default function () { console.log('foo');}
 // import-default.js
 import customName from './export-default';
 customName(); // 'foo'
Module – default vs normal
 import crc32 from 'crc32';
 // 對應的輸出
 export default function crc32(){}
 import { crc32 } from 'crc32';
 // 對應的輸出
 export function crc32(){};
 一個模塊只能有一個默認輸出,因此export deault命令只能使用一次。
Reference
 http://es6.ruanyifeng.com/#docs/promise
 http://es6.ruanyifeng.com/#docs/module

More Related Content

What's hot

改善程序设计技术的50个有效做法
改善程序设计技术的50个有效做法改善程序设计技术的50个有效做法
改善程序设计技术的50个有效做法crasysatan
 
第4章函数
第4章函数第4章函数
第4章函数
summerfeng
 
Intro to C++ Basic
Intro to C++ BasicIntro to C++ Basic
Intro to C++ Basic
Shih Chi Lin
 
Python 迴圈作業
Python 迴圈作業Python 迴圈作業
Python 迴圈作業
吳錫修 (ShyiShiou Wu)
 
Ch6 函式
Ch6 函式Ch6 函式
Ch6 函式
requiemformemories
 
The Evolution of Async Programming (GZ TechParty C#)
The Evolution of Async Programming (GZ TechParty C#)The Evolution of Async Programming (GZ TechParty C#)
The Evolution of Async Programming (GZ TechParty C#)jeffz
 
小谈Javascript设计模式
小谈Javascript设计模式小谈Javascript设计模式
小谈Javascript设计模式
Adam Lu
 
Python程式設計 - 串列資料應用
Python程式設計 - 串列資料應用 Python程式設計 - 串列資料應用
Python程式設計 - 串列資料應用
吳錫修 (ShyiShiou Wu)
 
Python變數與資料運算
Python變數與資料運算Python變數與資料運算
Python變數與資料運算
吳錫修 (ShyiShiou Wu)
 
期中考課輔
期中考課輔期中考課輔
期中考課輔
Garfield Wang
 
Python程式設計 - 迴圈作業
Python程式設計 - 迴圈作業Python程式設計 - 迴圈作業
Python程式設計 - 迴圈作業
吳錫修 (ShyiShiou Wu)
 
Ch08
Ch08Ch08
Ch08
jashliao
 
C語言 第五章 程式流程控制
C語言 第五章 程式流程控制C語言 第五章 程式流程控制
C語言 第五章 程式流程控制shademoon
 
C語言 第三章 03 運算子、運算元與運算式
C語言 第三章 03 運算子、運算元與運算式C語言 第三章 03 運算子、運算元與運算式
C語言 第三章 03 運算子、運算元與運算式shademoon
 
Python分支作業
Python分支作業Python分支作業
Python分支作業
吳錫修 (ShyiShiou Wu)
 
Scala再探
Scala再探Scala再探
Scala再探
afeihehe
 
Python 入門
Python 入門 Python 入門
Python 入門
Andy Yao
 
C語言 第二章 02 C的資料處理
C語言 第二章 02 C的資料處理C語言 第二章 02 C的資料處理
C語言 第二章 02 C的資料處理shademoon
 
Sql培训 (1)
Sql培训 (1)Sql培训 (1)
Sql培训 (1)
jhao niu
 

What's hot (20)

改善程序设计技术的50个有效做法
改善程序设计技术的50个有效做法改善程序设计技术的50个有效做法
改善程序设计技术的50个有效做法
 
第4章函数
第4章函数第4章函数
第4章函数
 
Intro to C++ Basic
Intro to C++ BasicIntro to C++ Basic
Intro to C++ Basic
 
Python 迴圈作業
Python 迴圈作業Python 迴圈作業
Python 迴圈作業
 
Ch6 函式
Ch6 函式Ch6 函式
Ch6 函式
 
The Evolution of Async Programming (GZ TechParty C#)
The Evolution of Async Programming (GZ TechParty C#)The Evolution of Async Programming (GZ TechParty C#)
The Evolution of Async Programming (GZ TechParty C#)
 
小谈Javascript设计模式
小谈Javascript设计模式小谈Javascript设计模式
小谈Javascript设计模式
 
Python程式設計 - 串列資料應用
Python程式設計 - 串列資料應用 Python程式設計 - 串列資料應用
Python程式設計 - 串列資料應用
 
Python變數與資料運算
Python變數與資料運算Python變數與資料運算
Python變數與資料運算
 
期中考課輔
期中考課輔期中考課輔
期中考課輔
 
Python程式設計 - 迴圈作業
Python程式設計 - 迴圈作業Python程式設計 - 迴圈作業
Python程式設計 - 迴圈作業
 
Ch08
Ch08Ch08
Ch08
 
C語言 第五章 程式流程控制
C語言 第五章 程式流程控制C語言 第五章 程式流程控制
C語言 第五章 程式流程控制
 
C語言 第三章 03 運算子、運算元與運算式
C語言 第三章 03 運算子、運算元與運算式C語言 第三章 03 運算子、運算元與運算式
C語言 第三章 03 運算子、運算元與運算式
 
Ch9
Ch9Ch9
Ch9
 
Python分支作業
Python分支作業Python分支作業
Python分支作業
 
Scala再探
Scala再探Scala再探
Scala再探
 
Python 入門
Python 入門 Python 入門
Python 入門
 
C語言 第二章 02 C的資料處理
C語言 第二章 02 C的資料處理C語言 第二章 02 C的資料處理
C語言 第二章 02 C的資料處理
 
Sql培训 (1)
Sql培训 (1)Sql培训 (1)
Sql培训 (1)
 

Viewers also liked

Vic weekly learning_20160504
Vic weekly learning_20160504Vic weekly learning_20160504
Vic weekly learning_20160504
LearningTech
 
JavsScript OOP
JavsScript OOPJavsScript OOP
JavsScript OOP
LearningTech
 
Type script
Type scriptType script
Type script
LearningTech
 
20150313 ian git
20150313 ian git20150313 ian git
20150313 ian git
LearningTech
 
Asp.net MVC DI
Asp.net MVC DIAsp.net MVC DI
Asp.net MVC DI
LearningTech
 
Knockout
KnockoutKnockout
Knockout
LearningTech
 
Meteor + Ionic Introduction
Meteor + Ionic IntroductionMeteor + Ionic Introduction
Meteor + Ionic Introduction
LearningTech
 
Node js
Node jsNode js
Node js
LearningTech
 
Howard type script
Howard   type scriptHoward   type script
Howard type script
LearningTech
 
ES2015 promise
ES2015 promiseES2015 promise
ES2015 promise
LearningTech
 
Report introduction to_npm_gulp_bower
Report introduction to_npm_gulp_bowerReport introduction to_npm_gulp_bower
Report introduction to_npm_gulp_bower
LearningTech
 
JavaScript
JavaScriptJavaScript
JavaScript
LearningTech
 
Knockoutjs – components
Knockoutjs – componentsKnockoutjs – components
Knockoutjs – components
LearningTech
 
Peggy sass vs scss
Peggy  sass vs scssPeggy  sass vs scss
Peggy sass vs scss
LearningTech
 
Node child process
Node child processNode child process
Node child process
LearningTech
 
Pm2
Pm2Pm2
Ken 20150306 心得分享
Ken 20150306 心得分享Ken 20150306 心得分享
Ken 20150306 心得分享
LearningTech
 
git command
git commandgit command
git command
LearningTech
 

Viewers also liked (18)

Vic weekly learning_20160504
Vic weekly learning_20160504Vic weekly learning_20160504
Vic weekly learning_20160504
 
JavsScript OOP
JavsScript OOPJavsScript OOP
JavsScript OOP
 
Type script
Type scriptType script
Type script
 
20150313 ian git
20150313 ian git20150313 ian git
20150313 ian git
 
Asp.net MVC DI
Asp.net MVC DIAsp.net MVC DI
Asp.net MVC DI
 
Knockout
KnockoutKnockout
Knockout
 
Meteor + Ionic Introduction
Meteor + Ionic IntroductionMeteor + Ionic Introduction
Meteor + Ionic Introduction
 
Node js
Node jsNode js
Node js
 
Howard type script
Howard   type scriptHoward   type script
Howard type script
 
ES2015 promise
ES2015 promiseES2015 promise
ES2015 promise
 
Report introduction to_npm_gulp_bower
Report introduction to_npm_gulp_bowerReport introduction to_npm_gulp_bower
Report introduction to_npm_gulp_bower
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Knockoutjs – components
Knockoutjs – componentsKnockoutjs – components
Knockoutjs – components
 
Peggy sass vs scss
Peggy  sass vs scssPeggy  sass vs scss
Peggy sass vs scss
 
Node child process
Node child processNode child process
Node child process
 
Pm2
Pm2Pm2
Pm2
 
Ken 20150306 心得分享
Ken 20150306 心得分享Ken 20150306 心得分享
Ken 20150306 心得分享
 
git command
git commandgit command
git command
 

Similar to Ian 20151002 es2015

Java script closures
Java script closuresJava script closures
Java script closures
skywalker1114
 
Java script closures
Java script closuresJava script closures
Java script closuresskywalker1114
 
重構—改善既有程式的設計(chapter 7)
重構—改善既有程式的設計(chapter 7)重構—改善既有程式的設計(chapter 7)
重構—改善既有程式的設計(chapter 7)Chris Huang
 
JavaScript 快速複習 2017Q1
JavaScript 快速複習 2017Q1JavaScript 快速複習 2017Q1
JavaScript 快速複習 2017Q1
Sheng-Han Su
 
Wind.js无障碍调试与排错
Wind.js无障碍调试与排错Wind.js无障碍调试与排错
Wind.js无障碍调试与排错jeffz
 
Js的国(转载)
Js的国(转载)Js的国(转载)
Js的国(转载)Leo Hui
 
學好 node.js 不可不知的事
學好 node.js 不可不知的事學好 node.js 不可不知的事
學好 node.js 不可不知的事
Ben Lue
 
認識 C++11 新標準及使用 AMP 函式庫作平行運算
認識 C++11 新標準及使用 AMP 函式庫作平行運算認識 C++11 新標準及使用 AMP 函式庫作平行運算
認識 C++11 新標準及使用 AMP 函式庫作平行運算
建興 王
 
Arduino程式快速入門
Arduino程式快速入門Arduino程式快速入門
Arduino程式快速入門
吳錫修 (ShyiShiou Wu)
 
Node.js开发体验
Node.js开发体验Node.js开发体验
Node.js开发体验QLeelulu
 
Arduino應用系統設計 - Arduino程式快速入門
Arduino應用系統設計 - Arduino程式快速入門Arduino應用系統設計 - Arduino程式快速入門
Arduino應用系統設計 - Arduino程式快速入門
吳錫修 (ShyiShiou Wu)
 
Keep your code clean
Keep your code cleanKeep your code clean
Keep your code clean
macrochen
 
Javascript Training
Javascript TrainingJavascript Training
Javascript Training
beijing.josh
 
淺談C#物件導向與DesignPattern.pdf
淺談C#物件導向與DesignPattern.pdf淺談C#物件導向與DesignPattern.pdf
淺談C#物件導向與DesignPattern.pdf
Brian Chou 周家禾
 
炎炎夏日學 Android 課程 - Part1: Kotlin 語法介紹
炎炎夏日學 Android 課程 -  Part1: Kotlin 語法介紹炎炎夏日學 Android 課程 -  Part1: Kotlin 語法介紹
炎炎夏日學 Android 課程 - Part1: Kotlin 語法介紹
Johnny Sung
 
Chapter 6 point (c)
Chapter 6 point (c)Chapter 6 point (c)
Chapter 6 point (c)
hhliu
 
Ch9 教學
Ch9 教學Ch9 教學
Ch9 教學
hungchiayang1
 
那 Angular 那 AJAX 那 RESTful
那 Angular 那 AJAX 那 RESTful那 Angular 那 AJAX 那 RESTful
那 Angular 那 AJAX 那 RESTful
功豪 魏
 

Similar to Ian 20151002 es2015 (20)

Java script closures
Java script closuresJava script closures
Java script closures
 
Java script closures
Java script closuresJava script closures
Java script closures
 
重構—改善既有程式的設計(chapter 7)
重構—改善既有程式的設計(chapter 7)重構—改善既有程式的設計(chapter 7)
重構—改善既有程式的設計(chapter 7)
 
JavaScript 快速複習 2017Q1
JavaScript 快速複習 2017Q1JavaScript 快速複習 2017Q1
JavaScript 快速複習 2017Q1
 
Wind.js无障碍调试与排错
Wind.js无障碍调试与排错Wind.js无障碍调试与排错
Wind.js无障碍调试与排错
 
Js的国(转载)
Js的国(转载)Js的国(转载)
Js的国(转载)
 
學好 node.js 不可不知的事
學好 node.js 不可不知的事學好 node.js 不可不知的事
學好 node.js 不可不知的事
 
認識 C++11 新標準及使用 AMP 函式庫作平行運算
認識 C++11 新標準及使用 AMP 函式庫作平行運算認識 C++11 新標準及使用 AMP 函式庫作平行運算
認識 C++11 新標準及使用 AMP 函式庫作平行運算
 
Arduino程式快速入門
Arduino程式快速入門Arduino程式快速入門
Arduino程式快速入門
 
Node.js开发体验
Node.js开发体验Node.js开发体验
Node.js开发体验
 
Arduino應用系統設計 - Arduino程式快速入門
Arduino應用系統設計 - Arduino程式快速入門Arduino應用系統設計 - Arduino程式快速入門
Arduino應用系統設計 - Arduino程式快速入門
 
Keep your code clean
Keep your code cleanKeep your code clean
Keep your code clean
 
Javascript Training
Javascript TrainingJavascript Training
Javascript Training
 
淺談C#物件導向與DesignPattern.pdf
淺談C#物件導向與DesignPattern.pdf淺談C#物件導向與DesignPattern.pdf
淺談C#物件導向與DesignPattern.pdf
 
炎炎夏日學 Android 課程 - Part1: Kotlin 語法介紹
炎炎夏日學 Android 課程 -  Part1: Kotlin 語法介紹炎炎夏日學 Android 課程 -  Part1: Kotlin 語法介紹
炎炎夏日學 Android 課程 - Part1: Kotlin 語法介紹
 
Chapter 6 point (c)
Chapter 6 point (c)Chapter 6 point (c)
Chapter 6 point (c)
 
Ch 8
Ch 8Ch 8
Ch 8
 
Banquet 52
Banquet 52Banquet 52
Banquet 52
 
Ch9 教學
Ch9 教學Ch9 教學
Ch9 教學
 
那 Angular 那 AJAX 那 RESTful
那 Angular 那 AJAX 那 RESTful那 Angular 那 AJAX 那 RESTful
那 Angular 那 AJAX 那 RESTful
 

More from LearningTech

vim
vimvim
PostCss
PostCssPostCss
PostCss
LearningTech
 
ReactJs
ReactJsReactJs
ReactJs
LearningTech
 
Docker
DockerDocker
Docker
LearningTech
 
Semantic ui
Semantic uiSemantic ui
Semantic ui
LearningTech
 
node.js errors
node.js errorsnode.js errors
node.js errors
LearningTech
 
Process control nodejs
Process control nodejsProcess control nodejs
Process control nodejs
LearningTech
 
Expression tree
Expression treeExpression tree
Expression tree
LearningTech
 
SQL 效能調校
SQL 效能調校SQL 效能調校
SQL 效能調校
LearningTech
 
flexbox report
flexbox reportflexbox report
flexbox report
LearningTech
 
Reflection & activator
Reflection & activatorReflection & activator
Reflection & activator
LearningTech
 
Peggy markdown
Peggy markdownPeggy markdown
Peggy markdown
LearningTech
 
20160415ken.lee
20160415ken.lee20160415ken.lee
20160415ken.lee
LearningTech
 
Peggy elasticsearch應用
Peggy elasticsearch應用Peggy elasticsearch應用
Peggy elasticsearch應用
LearningTech
 
Expression tree
Expression treeExpression tree
Expression tree
LearningTech
 
Vic weekly learning_20160325
Vic weekly learning_20160325Vic weekly learning_20160325
Vic weekly learning_20160325
LearningTech
 
D3js learning tips
D3js learning tipsD3js learning tips
D3js learning tips
LearningTech
 
Vic weekly learning_20151127
Vic weekly learning_20151127Vic weekly learning_20151127
Vic weekly learning_20151127
LearningTech
 
Mocha.js
Mocha.jsMocha.js
Mocha.js
LearningTech
 
R language
R languageR language
R language
LearningTech
 

More from LearningTech (20)

vim
vimvim
vim
 
PostCss
PostCssPostCss
PostCss
 
ReactJs
ReactJsReactJs
ReactJs
 
Docker
DockerDocker
Docker
 
Semantic ui
Semantic uiSemantic ui
Semantic ui
 
node.js errors
node.js errorsnode.js errors
node.js errors
 
Process control nodejs
Process control nodejsProcess control nodejs
Process control nodejs
 
Expression tree
Expression treeExpression tree
Expression tree
 
SQL 效能調校
SQL 效能調校SQL 效能調校
SQL 效能調校
 
flexbox report
flexbox reportflexbox report
flexbox report
 
Reflection & activator
Reflection & activatorReflection & activator
Reflection & activator
 
Peggy markdown
Peggy markdownPeggy markdown
Peggy markdown
 
20160415ken.lee
20160415ken.lee20160415ken.lee
20160415ken.lee
 
Peggy elasticsearch應用
Peggy elasticsearch應用Peggy elasticsearch應用
Peggy elasticsearch應用
 
Expression tree
Expression treeExpression tree
Expression tree
 
Vic weekly learning_20160325
Vic weekly learning_20160325Vic weekly learning_20160325
Vic weekly learning_20160325
 
D3js learning tips
D3js learning tipsD3js learning tips
D3js learning tips
 
Vic weekly learning_20151127
Vic weekly learning_20151127Vic weekly learning_20151127
Vic weekly learning_20151127
 
Mocha.js
Mocha.jsMocha.js
Mocha.js
 
R language
R languageR language
R language
 

Ian 20151002 es2015

Editor's Notes

  1. 塊級作用域的出現,實際上使得獲得廣泛應用的立即執行匿名函數(IIFE)
  2. 嚴格地說,不是可遍歷的結構,參見《Iterator》一章),那麼將會報錯 解構賦值允許指定默認值
  3. 變量的取值由它的位置決定;而對象的屬性沒有次序,變量必須與屬性同名,才能取到正確的值。
  4. 字符串的解構賦值
  5. 上面代碼是為函數move的參數指定默認值,而不是為變量x和y指定默認值,所以會得到與前一種寫法不同的結果。
  6. ES6模塊的設計思想,是儘量的靜態化,使得編譯時就能確定模塊的依賴關係,以及輸入和輸出的變量。CommonJS和AMD模塊,都只能在運行時確定這些東西。比如,CommonJS模塊就是對象,輸入時必須查找對象屬性。 ES6的模塊自動採用嚴格模式 "use strict"
  7. (用大括號表示)裡面指定要從其他模塊導入的變量名。大括號裡面的變量名,必須與被導入模塊(profile.js)對外接口的名稱相同
  8. 整體輸入
  9. module命令可以取代import語句,達到整體輸入模塊的作用。 module命令後面跟一個變量,表示輸入的模塊定義在該變量上。
  10. import命令可以為該匿名函數指定任意名字