forked from nhattruongniit/learn-javascripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththis.js
More file actions
73 lines (66 loc) · 1.37 KB
/
this.js
File metadata and controls
73 lines (66 loc) · 1.37 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
let numbers = {
numberA: 5,
numberB: 10,
sum: function () {
console.log('1', this);
function calculate() {
console.log('2');
console.log('sum', this.numberA + this.numberB);
}
return calculate();
}
};
numbers.sum();
var user = {
tournament: 'The Masters',
data: [{
name: "Truong",
age: 18
}, {
name: 'Minh',
age: 18
}],
clickHandler: function() {
var self = this;
this.data.forEach(function(person) {
console.log(person.name + " is playing at " + self.tournament)
})
}
}
user.clickHandler();
/*! bind ==================*/
var name = "Peter";
var HocSinh = {
name: 'John',
printName: function() {
console.log(this.name);
}
}
var printSv = HocSinh.printName.bind(HocSinh);
printSv();
/*! this in borrow method ==============*/
var gameController = {
scores: [10, 15, 20],
avgScore: null,
players: [{
name: 'Tommy',
playerID: 987,
age: 23
}, {
name: 'Jason',
playerID: 412,
age: 24
}]
}
var appController = {
scores: [30, 40, 50],
avgScore: null,
compterAvg: function() {
var sumOfScores = this.scores.reduce(function(prev, cur, index, array) {
return prev + cur
});
this.avgScore = sumOfScores / this.scores.length
}
}
appController.compterAvg.apply(gameController);
console.log(gameController.avgScore);