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
49 lines (40 loc) · 1.25 KB
/
this.js
File metadata and controls
49 lines (40 loc) · 1.25 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
/* The for principles of "this";
newFunction();
function newFunction() {
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1. The 'this' keyword defaults to the global scope which is the window or console.
* 2. The 'this' keyword is implicitly refers to the object in which it is being called.
* 3. When a new object is created by the 'new' keyword, the 'this' keyword refers to the new object.
* 4. When using the 'call' or 'apply' function, the 'this' context for the 'this keyword is explicity defined.
*
* write out a code example of each explanation above
*/
// Principle 1: // code example for Window Binding:
console.log("This is:, " this);
// Principle 2: code example for Implicit Binding
let live = {
money: true,
shop: function() {
if (this.money = true) {
console.log("Let's shop!")
}
}
}
live.shop();
// Principle 3: code example for New Binding
function live () {
this.name = name;
this.money = true;
this.shop = function () {
if (this.money = true) {
console.log(`Let ${this.name} shop!`);
}
}
}
const mylive = new live ({
name: 'June'
})
mylive.shop();
// Principle 4: code example for Explicit Binding