-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_methods.js
52 lines (40 loc) · 1 KB
/
object_methods.js
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
var obj = {
name: "vishal",
talk: function() {
console.log("say hi");
}
}
console.log(obj);
obj.talk()
obj.talk = function() {
console.log("how do you do !");
}
obj.talk();
var newobj = {
name: 'vishal',
talk() {
console.log("bon joure, i am "+this.name);
//console.log("bon joure, i am "+newobj.name); //this is same as above statement, where this = newObj
//but above line is not proper way as the outer variable name may change in assignment
}
}
newobj.talk();
var nobj = newobj;
newobj = null;
nobj.talk(); //this results in problem as one of console.log tries to access name with name 'newobj'
//this is not bound to any fix object, infact this is evaluated during runtime, depending upon which obj is calling
function sayHi() {
console.log(this);
}
sayHi();
nobj.talk = sayHi;
nobj.talk();
var arr_obj = {
user: "vishal",
sayHi() {
console.log("hey " + this.user);
},
};
var hi = arr_obj.sayHi;
var user = "vishal";
hi();