宣告變數
變數 |
說明 |
修改 |
var |
在{}裡宣告變數,會影響全域變數(window) |
可以被修改 |
let |
在{}裡宣告變數,只存留在{}裡 |
可以被修改 |
const |
在{}裡宣告變數,只存留在{}裡 |
不能被修改,但物件和陣列除外 |
使用Object.freeze(),括弧裡面放const宣告的物件可以凍結
1
2
|
let apple = "蘋果";
//宣告變數 變數名稱 = 賦予的值;
|
宣告的變數會儲存在window裡
區域變數(Area Variable)
1
2
3
4
5
6
|
function sayApple(){
let apple = "蘋果";
console.log(apple); //蘋果
}
sayApple();
console.log(apple); //undefined
|
function()執行完後,宣告也會跟著結束
全域變數(Global Variable)
1
2
3
4
5
6
|
let apple;
function sayApple(){
apple = "蘋果";
}
sayApple();
console.log(apple); //蘋果
|
若要讓function()執行完後,取得變數,則在function外先做宣告
函式
1
2
3
4
5
6
7
8
9
|
function sayHi(){
console.log("Hello World!!");
}
//宣告函式 函式名稱 (帶參數){
// 執行動作
//};
sayHi();
//執行函式
|
帶參數寫法:
1
2
3
4
5
6
7
8
9
|
function sayHi(item){
console.log(item);
}
//宣告函式 函式名稱 (帶參數){
// 執行動作
//};
sayHi("Hello World!!");
//執行函式
|
函式具有hoisting效果,不論執行先後,function都會先執行
javascript的保留字
關鍵字 |
關鍵字 |
關鍵字 |
關鍵字 |
arguments |
await* |
break |
case |
catch |
class* |
const* |
continue |
debugger |
default |
delete |
do |
else |
enum* |
eval |
export* |
extends* |
false |
finally |
for |
function |
if |
implements |
import* |
in |
instanceof |
interface |
let* |
new |
null |
package |
private |
protected |
public |
return |
static |
super* |
switch |
this |
throw |
try |
typeof |
var |
void |
while |
with |
yield |
|
宣告變數或函式時若使用保留字會出錯
其他相關