大家好,这里是某昨,好久不见。

动机

动动动动动机

为了拉人帮忙填坑,而会Javascript的同道又太少,某昨决定自己拟一篇教程,用来培训挖掘能够帮忙填坑的dalao

基本语法

以下内容仅供参考。

If

1
2
3
4
5
6
7
if (statement) {
// Code here.
} else if (statement) {
// Code here.
} else {
// Code here.
}

Switch

1
2
3
4
5
6
7
8
9
10
11
switch (statement) {
case A:
// Code here.
break;
case B:
// Code here.
break;
default:
//Code here.
break;
}

?:

1
statement ? do_if_true : do_if_false;

while

1
2
3
while (statement) {
// Code here.
}

for

1
2
3
for (initial; condition; operation) {
// Code here.
}

for … in

1
2
3
4
5
// i must be declared, or declared in `for`
for (i in array) {
// i becomes the array
array[i] = xxx;
}

for … of

1
2
3
4
for (item of array) {
// item here is similiar to array[i] above.
item = xxx;
}

declaration

1
2
3
4
5
6
7
8
// deprecated
var variable_name = default_value;

// use now
let variable_name = default_value;

// the content of the variable can't be changed
const variable_name = default_value;

comments

1
2
3
4
5
// single-line

/**
* multiple lines
* /

Objects

1
2
3
4
5
6
7
const obj = {
a: 1,
b: 2
};

// here `obj` only saves the reference of that variable.
obj.a = 3;

function

1
2
3
4
function func(arg1, arg2, ...) {
// here arguments == [arg1, arg2, ...]
return arguments;
}

lambda

1
2
3
4
5
6
7
// arrow function
const func = () => {
// it's a function
};

// a function returns 2i
const func_with_return = i => i * 2;

string

1
2
3
4
5
const single = 'single';
const double = "double";

// equals to single + ' and ' + 'double'
const with_another_variable = `${single} and ${double}`;