Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
ES6-函示與作用域
Search
KuoGeorge
May 04, 2018
37
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
ES6-函示與作用域
讀書會分享 - 從es6開始的 Javascript 學習生活
KuoGeorge
May 04, 2018
More Decks by KuoGeorge
See All by KuoGeorge
世恩- 陣列
georgekuo
0
25
Featured
See All Featured
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.4k
Speed Design
sergeychernyshev
33
2.2k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
500
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
270
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.6k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
330
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
How to Ace a Technical Interview
jacobian
280
24k
Mobile First: as difficult as doing things right
swwweet
225
10k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Producing Creativity
orderedlist
PRO
348
41k
First, design no harm
axbom
PRO
2
1.3k
Transcript
從ES6開始的Javascript學習生活 - 函式與作⽤域 - @郭喬治
函式的基本語法結構 //匿名函式 function() {} //有名稱的函式 function foo() {}
函式的基本語法結構 //匿名函式 function() {} //沒有函數名稱,通常⽤來當作⼀個指定值
函式的基本語法結構 //匿名函式 function() {} //沒有函數名稱,通常⽤來當作⼀個指定值 var foo = function ()
{}
箭頭函式(Arrow Function) const sum = ( a, b ) =>
a + b
箭頭函式(Arrow Function) const sum = (a, b) => a +
b const sum = function(a, b){ return a + b; }
箭頭函式(Arrow Function) const sum = (a, b) => a +
b const sum = function(a, b){ return a + b; }
箭頭函式(Arrow Function) ([param] [, param]) => { statements } param
=> expression statements vs expression
箭頭函式(Arrow Function) ([param] [, param]) => { statements } //
花括號 { } 是有意義的,如果函式沒回傳東⻄就要使⽤花括號, 反之則不使⽤。 param => expression // 只有單⼀個參數時,可不⽤()。
箭頭函式(Arrow Function) // 未加花括號 (x, y) => x+y
箭頭函式(Arrow Function) // 未加花括號 (x, y) => x+y function (x,
y) { return x + y; };
箭頭函式(Arrow Function) // 未加花括號 (x, y) => x+y function (x,
y) { return x + y; }; // 有加花括號 (x, y) => { x+y }
箭頭函式(Arrow Function) // 未加花括號 (x, y) => x+y function (x,
y) { return x + y; }; // 有加花括號 (x, y) => { x+y } (function (x, y){ x + y; }
箭頭函式(Arrow Function) param => expression // 只有單⼀個參數時,可不⽤()。
箭頭函式(Arrow Function) param => expression // 只有單⼀個參數時,可不⽤()。 (x) => x+1
箭頭函式(Arrow Function) param => expression // 只有單⼀個參數時,可不⽤()。 (x) => x+1
x => x+1
箭頭函式(Arrow Function) param => expression // 只有單⼀個參數時,可不⽤()。 (x) => x+1
(function (x) { return x + 1; } x => x+1
預設參數值 function sum(x, y){ x = x || 10; y
= y || 20; console.log( x + y); } sum(); //30 sum(2, 3); //5 sum(2); //22 sum(null, 3); //13 ES5
預設參數值 function sum(x, y){ x = x || 10; y
= y || 20; console.log( x + y); } sum(); //30 sum(2, 3); //5 sum(2); //22 sum(null, 3); //13 function sum(x = 11, y =6){ console.log( x + y); } sum(); //17 sum(2, 3); //5 sum(2); //8 sum(null, 3); //3 ES5 ES6
參數傳入運算式 function bar(val) { return y + val; } function
foo(x = y+3, z = bar(x)) { console.log( x + z); } var y = 5; foo();
函式作為傳入參數 const addOne = function(value){ return value + 1 }
const addOneAndTwo = function(value, fn){ return fn(value) + 2 } console.log(addOneAndTwo(10, addOne)) //13
函式不固定傳入參數 函數傳入參數時,沒有預設值的時候會視為undefined值
函式不固定傳入參數 function sum(x, y, z) { return x+y+z } console.log(sum(1,
2, 3)) //6 console.log(sum(1, 2)) //NaN console.log(sum(1, 2, 3, 4)) //6 console.log(sum(‘1’, ‘2’, ‘3’)) //123 console.log(sum(‘1’, ‘2’)) //12undefined console.log(sum(‘1’, ‘2’, ‘3’, ‘4’)) //123 函數傳入參數時,沒有預設值的時候會視為undefined值