-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path011-arrow-function-and-this-scope.html
56 lines (52 loc) · 1.11 KB
/
011-arrow-function-and-this-scope.html
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
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.29/browser.js"></script>
<script type="text/babel">
//_this
var person = {
first: "Doug",
actions: ['bike', 'hike', 'ski', 'surf'],
printActions: function() {
var _this= this;
this.actions.forEach(function(action) {
var str = _this.first + " likes to " + action;
console.log(str);
});
}
};
person.printActions();
//bind method
/*
var person = {
first: "Doug",
actions: ['bike', 'hike', 'ski', 'surf'],
printActions: function() {
this.actions.forEach(function(action) {
var str = this.first + " likes to " + action;
console.log(str);
}.bind(this));
}
};
person.printActions();
*/
//arrow function
/*
var person = {
first: "Doug",
actions: ['bike', 'hike', 'ski', 'surf'],
printActions() {
this.actions.forEach(action => {
var str = this.first + " likes to " + action;
console.log(str);
});
}
};
person.printActions();
*/
</script>
<title>Arrow Functions and 'this'</title>
</head>
<body>
</body>
</html>