forked from bloominstituteoftechnology/JavaScript-II-Mini
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththis.js
More file actions
59 lines (54 loc) · 1.41 KB
/
this.js
File metadata and controls
59 lines (54 loc) · 1.41 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
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1. Window/global score. This points to that object.
* 2. Object before the dot becomes this.
* 3. the object where the new keyword called. new binds this to the new object
* 4. .call .apply
*
* write out a code example of each explanation above
*/
// Principle 1
// function sayName(name) {
// console.log(this);
// }
// console.log(this);
// code example for Window Binding
// Principle 2
const myObj = {
greeting: 'Hello',
sayHello: function (name) {
console.log(`${this.greeting} ${name}`);
console.log(this);
},
};
// myObj.sayHello('Ryan')
// const sayNameFunc = (obj) => {
// obj.sayName = function()
// {
// console.log(`Hello my name is ${this.name}`)
// }
// }
// const me = { name: "Aaron"};
// const you = { name: 'Freddy' };
// sayNameFunc(me);
// sayNameFunc(you);
// console.log(me);
// me.sayName();
//you.sayName();
// code example for Implicit Binding
// Principle 3
function CordiaPerson(greeter) {
this.greeting = 'Hello';
this.greeter = greeter;
this.speak = function() {
console.log(this.greeting + this.greeter);
console.log(this);
};
}
const jerry = new CordiaPerson('Newman');
const newman = new CordiaPerson('Jerry');
jerry.speak();
// code example for New Binding
// Principle 4
// code example for Explicit Binding