-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbind.js
More file actions
66 lines (52 loc) · 1.61 KB
/
Copy pathbind.js
File metadata and controls
66 lines (52 loc) · 1.61 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
66
let dog = {
sound: 'woooof',
talk: function () {
console.log(this.sound);
}
}
/**
* Following line of code prints woooof without need to bind and object
* because **this** refers to the object's scope which is calling,
* dog in this case.
*/
dog.talk();
let rottweiler = dog.talk
rottweiler() // prints undefined, because **this** points to window object, which is the actual scope
/**
* to use print correctly what rottweiler object has, is needed bind dog
* object passing it as paremeter, now **this** points to the passed object (dog)
* instead of window.
*/
rottweiler.bind(dog)(); // parenthesis at the end are used to call the function
/**
* *********************************************************************************************************
*/
/**
* function used as dummy, if we cal it without binding or calling usign an object
* it is gointg to print **undefined** because **this** is pointing or refering to window object
* and that object does not have a property called **say **
*/
(saySomething = function(){
console.log(this.say);
})();
/**
* this object has talk property which use **saySomething** function as its talk property value,
*/
let luis = {
talk: saySomething,
say: 'hello I am Luis..... cool'
}
/**
* invoque talk property of luis object, and it will run correctly,
* prints 'hello I am Luis..... cool', because its beign called from luis object
* so **this** points to luis object
*/
luis.talk();
/**
* so.... What is going to happen in the next lines of code?
*/
let juan = {
tell: luis.talk,
say: 'Hello I am Juan.... coolest than luis !!'
}
juan.tell()