Repository navigation
Expand file tree
/
Copy pathNotes.txt
More file actions
119 lines (90 loc) · 2.12 KB
/
Copy pathNotes.txt
File metadata and controls
119 lines (90 loc) · 2.12 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
function User(obj) {
// this = {};
this.firstName = obj.firstName;
this.lastName = obj.lastName;
this.lovesJavascript = true;
this.saysHi = function(person) {
return `${this.firstName} says hi to ${person}!`
};
// return this
};
let Dan = new User({
firstName: 'Dan',
lastName: 'Frehner'
})
let Corey = new User({
firstName: 'Corey',
lastName: 'Kohler'
})
// 4 'this' bindings
//New this binding
Dan.saysHi('Demetri')
Corey.saysHi('Oliver')
// Window binding
function saySomething(){
console.log(this)
}
setTimeout( function() {console.log(this)}, 200)
// implicit
let car = {
model: 'Mustang',
vroom: function(){
return `${this.model} goes vrrrooooommm!`
}
}
car.vroom()
// Explicit
// .call .apply .bind
const dan = {
name: 'Dan'
}
const venky = {
name: 'Venky'
}
const skillz = ['Javascript', 'HTML', 'LESS']
function introduce(s1, s2, s3){
console.log("The this context:", this)
return `My name is ${this.name} and I like to program in ${s1}, ${s2}, ${s3}`
}
introduce.apply(dan, skillz)
// .call(venky)
// Immediatly invoke the function
const venkySkillz = introduce.bind(venky)
const danSkillz = introduce.bind(dan)
venkySkillz('Javascript', 'CSS', 'C++')
danSkillz('Javascript', 'Ruby', 'C#')
// Class constructors and prototypal inheritance
function Animal(AnimalObj){
// this = {}
this.species = AnimalObj.species;
this.sound = AnimalObj.sound;
this.isADog = AnimalObj.isADog;
// return this
}
Animal.prototype.speak = function(){
return `The ${this.species} says ${this.sound}!`
}
const chester = new Animal('Cheetah', '...', false)
chester.speak()
function Dog(breed) {
//this = {}
Animal.call(this, 'Dog', 'Woof!', true)
this.breed = breed;
// return this
}
function Cat(breed) {
// this = {}
Animal.call(this, 'Cat', 'Meow', false);
this.breed = breed;
// return this
}
Dog.prototype = Object.create(Animal.prototype);
const Lab = new Dog('Lab');
Lab.speak()
Cat.prototype = Object.create(Animal.prototype)
Cat.prototype.speak = function(){
console.log("I'm I cat!!!")
}
const Tabby = new Cat('Tabby')
Tabby.speak()
// Lab.speak()