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 (47 loc) · 1.46 KB
/
this.js
File metadata and controls
59 lines (47 loc) · 1.46 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 binding:- when in the global scope in the browser, this refers to 'window'
* 2. implicit binding:- when a function is called by a preceding dot, this refers to the object before the dot.
* 3. new binding:- this in this case refers to the object that was created using the constructor function
* 4. explicit binding:- using call, apply and bind methods
*
* write out a code example of each explanation above
*/
console.log('hello world!');
// Principle 1
// code example for Window Binding
function newFunction (){
console.log(this);
}
newFunction();
// Principle 2
// code example for Implicit Binding
const newObj = {
name: 'nedu',
age: 20,
dateOfBirth: function(){
console.log(`My date of birth is current year - ${this.age}`)
}
}
newObj.dateOfBirth();
// Principle 3
// code example for New Binding
let newFunction = function (name, age) {
this.name = name;
this.age = age;
}
let anotherObject = new newFunction('nedu', 20);
// Principle 4
// code example for Explicit Binding
let newObj = {
name: 'nedu',
age: 20
}
let newArray = ['dsf', 'ewfd', 'dfvdc'];
let newFunction = function () {
console.log(`My name is ${this.name} and I am ${this.age} years old.`);
}
newFunction.call(newObj, ...newArray);
newFunction.apply(newObj, newArray);
let anotherNewFunction = newFunction.bind(newObj, ...newArray);