You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* in your own words. explain the four principle for the "this" keyword below.
3
-
*
4
-
* 1.
5
-
* 2.
6
-
* 3.
7
-
* 4.
8
-
*
3
+
* this keyword have 4 scopes.
4
+
* 1. Global Scope
5
+
in global scope the this keyword points to the global object in system it is windows object and in a browser its browser object.
6
+
if we have not binded the object to any other object implicitly its this scope will be the global.
7
+
8
+
* 2. Implicit binding scope
9
+
implicit binding is when we invoke this keyword within a method of any object. In this case every first child method, its this will point to the object itself.
10
+
if we have any inner object inside a object the this points to the global object.
11
+
12
+
* 3. Explicit binding scope
13
+
Explicit binding is when we use call , apply or bind to any function. we define what the this keyword will point to in the first parameter of call, apply and bind.
14
+
15
+
* 4. New keyword binding scope
16
+
this is when we use a constructor function to create new objects, and then, this keyword points to the parameter passed into the constructor.
9
17
* write out a code example of each explanation above
10
18
*/
11
19
@@ -14,15 +22,63 @@ console.log('hello world!');
14
22
// Principle 1
15
23
16
24
// code example for Window Binding
25
+
console.log(this)// here this is the window object.
17
26
18
27
// Principle 2
19
28
20
29
// code example for Implicit Binding
30
+
letnewObj={
31
+
name: 'imran',
32
+
age: 30,
33
+
getAgeAndName: function(){
34
+
returnthis.name+' '+this.age;// here this points to the newObj(parent object);
35
+
child: functionany(){
36
+
console.log(this);// but here this will be the global object(window);
37
+
}
38
+
}
39
+
}
21
40
22
41
// Principle 3
23
42
24
43
// code example for New Binding
44
+
functionConstExample(param1,param2){
45
+
this.name=param1;// here this points to the parameter passed
0 commit comments