js語法

宣告

var 宣告一個可隨意更改其內容的變數
let 宣告一個可隨意更改其內容的區塊區域變數
const 宣告一個只可讀取的不可變常數
window.thinggggggggg 全域變數
thing=['thing1','thing2'] 陣列
wtf = { wtf1: 'lol',wtf1: 'lol' }; 物件字面值 Object literals

範例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var a=1;
console.log(a);

let x=1;
console.log(x);

var window.z=1;
console.log(window.z);

var lol = ['lol1', , 'lol2'];
console.log(lol[0]);

var wtf = { wtf1: 'lol', wtf2: function('lol'), wtf3: lol };
console.log(lol.wtf1);

if else

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if (x===y){
yes();
}
else{
no();
}


if (x===y) {
yes();
}
else if (x===z) {
yessss();
}
else if (x===0) {
yesssssssss();
}
else {
no();
}

switch

1
2
3
4
5
6
7
8
9
10
11
12
13
switch (input) {
case "1":
console.log("1");
break;
case "2":
console.log("2");
break;
case "3":
console.log("3");
break;
default:
console.log("0");
}

try

1
2
3
4
5
6
7
8
9
try { //正常
if(0=0);
}
catch (e) { //崩潰
logMyErrors(e);
}
finally {
console.log("yeeeeee"); //一定會執行
}

for

1
2
3
for (var i = 0; i < 10; i++) {
console.log(i);
}

for in

1
2
3
4
var object = [1, 2, 3];
for (input in object) {
console.log(input) //0 1 2 (位置)
}

for of

1
2
3
4
var object = [1, 2, 3];
for (input of object) {
console.log(input) //1 2 3 (內容)
}

do while

1
2
3
4
5
  var i=0;
do { //先執行
i += 1;
console.log(i);
} while (i < 2); //後檢查

while

1
2
3
4
5
6
7
while (true) {   //無限迴圈
console.log("AAA");
}

while (var n < 3) { //直到 false
n++;
}

迴圈例外處理
brake 終止迴圈
confinue 跳回迴圈判斷

function

1
2
3
function lol(fuckthing){
return fuckthing;
}