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
63 lines (54 loc) · 1.55 KB
/
this.js
File metadata and controls
63 lines (54 loc) · 1.55 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
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1. Window binding is when you call the this. on the global scope.
* 2. Implicit binding is when you call the this. inside of an object.
* 3. You use the new keyword when creating an instance of an object class.
* 4. Call/Apply temporarily binds this. to the object passed into it.
*
* write out a code example of each explanation above
*/
console.log('hello world!');
// Principle 1
// code example for Window Binding
function sayName(name) {
console.log(this);
return name;
}
// Principle 2
// code example for Implicit Binding
const myObj = {
greeting: 'Hello',
sayHello: function(name) {
console.log(`${this.greeting} my name is ${name}`);
console.log(this);
}
};
// Principle 3
function CordialPerson(greeter) {
this.greeting = 'Hello';
this.greeter = greeter.name;
this.speak = function() {
console.log(`Hello, ${this.greeter}`);
console.log(this);
};
}
const jerry = new CordialPerson({ name: 'Newman' });
const newman = new CordialPerson({ name: 'Jerry' });
// Principle 4
jerry.speak.call(newman);
newman.speak.apply(jerry);
function Animal(object) {
this.type = object.type;
this.name = object.name;
this.sound = object.sound;
this.speak = function() {
return this.sound;
};
}
const doggo = new Animal({ type: 'Dog', name: 'Murphy', sound: 'Woof' });
const liger = new Animal({
type: 'LionTiger',
name: 'Leery',
sound: 'Roar/Meow'
});