1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
/** 範例一 **/
const a = [1,2,3];
const b = a.map(function(par1){
return par1 * 2;
})
console.log(b); //[2,4,6]
/** 範例一(ES6) **/
const a = [1,2,3];
const b = a.map(par1 => par1 * 2);
console.log(b); //[2,4,6]
/** 範例二 **/
const a = function(){
const nums = Array.from(arguments);
//Array.from()能將[類陣列]轉為[陣列]
const total = nums.reduce(function(par1,par2){
//第一個參數會帶0,第二個參數會帶入當前的值
return par1+par2;
},0);
console.log(total); //15
return total / nums.length;
}
console.log(a(1,2,3,4,5)); //3
/** 範例二(ES6) **/
const a = (...arg) => arg.reduce((par1,par2) => par1+par2 / arg.length,0);
console.log(a(1,2,3,4,5)); //3
/** 範例三 **/
const a = {
data:{},
getData: function(){
const vm = this;
$.ajax({
url: "https://randomuser.me/api/",
dataType: "json",
success: function(data){
vm.data = data.results[0];
console.log("a.data",a.data);
}
});
}
}
a.getData();
/** 範例三(ES6) **/
const a = {
data:{},
getData: function(){
$.ajax({
url: "https://randomuser.me/api/",
dataType: "json",
success: (data) => {
this.data = data.results[0];
console.log("a.data",a.data);
}
});
}
}
a.getData();
|