This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var status = 'failed'; // 全域變數 | |
function getStatus() { | |
var status = 'ok'; // 區域變數 | |
return status | |
} | |
console.log(getStatus()) | |
console.log(status) | |
// 從第一行到第七行稱為全域範圍 | |
// 從第二行到第五行稱為區域範圍 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
status = 'failed'; // 全域變數 | |
function getStatus() { | |
status = 'ok'; // 區域變數 | |
return status | |
} | |
console.log(getStatus()) // 印出 ok | |
console.log(status) // 預期要印出 failed,結果卻印出 ok |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log(status) | |
var status = 'failed'; | |
function getStatus() { | |
console.log(status) | |
var status = 'ok'; | |
return status | |
} | |
// 第一行會印出 failed | |
// 第四行會印出 undefined (未定義) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function vipName() { | |
let customer = 'Acer' | |
} | |
console.log(customer) // 會出現 customer is not defined | |
function cityName() { | |
const city = 'Taipei' | |
} | |
console.log(city) // 會出現 city is not defined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var x = 1; | |
{ | |
var x = 2; | |
console.log(x) | |
} | |
console.log(x) | |
// console 會印出 2 跟 2 | |
let x = 1; | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(參數,...) => { | |
程式敘述; | |
return value | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mathDouble = function(a) { // 函式表達式 | |
return a * 2; | |
} | |
console.log(mathDouble (10)) // 呼叫函式 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mathDouble = (a) => { // 箭頭函式表達式寫法 | |
return a * 2; | |
} | |
console.log(mathDouble (10)) // 呼叫函式 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mathDouble = (a) => a * 2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const vipName = (acer) => console.log('Welcome' + acer); | |
const vipName = acer => console.log('Welcome' + acer); // 只有一個參數,可以不用加上括號 | |
const vipName = () => console.log('Welcome'); // 沒有參數,括號必須保留 |
OlderNewer