-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy paththis.js
More file actions
65 lines (48 loc) · 1.39 KB
/
this.js
File metadata and controls
65 lines (48 loc) · 1.39 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
/* The four principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1. Window binding - using this as it refers to the window object
* 2. Implicit binding - within the context of an object, this represents an object
* 3. New Binding - used in the creation of objects from constructors
* 4. Explicit binding - use of .call .apply or .bind to assign an object for a functions this reference, do exactly this but with each item of an array, or assign an object for a functions this reference and create a new function.
*
* 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;
}
sayName('billy');
// Principle 2
// code example for Implicit Binding
const animal = {
Name: dog,
Fur: brown,
Command: "woof",
Bark: function() {
console.log(this.command);
}
}
animal.bark();
// Principle 3
// code example for New Binding
Var Animal = function(coler, name, type) {
This.color = color;
This.name = name;
This.type = type;
}
var bird = new Animal ('black', 'Toucan Sam', 'Bird');
// Principle 4
// code example for Explicit Binding
// using .call
Let me = {
Name: 'Nalee',
Age: 23;
}
Let sayName = function() {
console.log('My Name is: ' + this.name);
}
sayName.call(me);