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
39
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
ES6-函示與作用域
讀書會分享 - 從es6開始的 Javascript 學習生活
KuoGeorge
May 04, 2018
Transcript
23 slides
從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值
More Decks by KuoGeorge
See All by KuoGeorge
世恩- 陣列
georgekuo
0
26
Featured
See All Featured
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
The Spectacular Lies of Maps
axbom
PRO
1
1.1k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
2
410
Art, The Web, and Tiny UX
lynnandtonic
304
22k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.3k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
530
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
2
6.5k
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
RailsConf 2023
tenderlove
30
1.6k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
380
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
1.1k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k