-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.js
More file actions
116 lines (87 loc) · 1.59 KB
/
Copy pathcontext.js
File metadata and controls
116 lines (87 loc) · 1.59 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
foo = {
bar: function() {
console.log(this);
},
baz: function(cb) {
cb();
}
}
//scope != context: context depends on function invocation
/*
foo.bar();
foo.baz(foo.bar);
*/
function f() {
var b = "here is a b";
this.q = function() {
console.log(b);
}
this.q();
}
/*
f();
q();//we can call q!
//since JS is OO:
f = new f();
f.q();
*/
//Function invocation and scope:
function g() {
console.log(this);
}
console.log(this);
// g();
/*
When you call a function by name using () the context is set to the global context-ALWAYS.
*/
//Object method vs a function:
z = {
h: function() {
console.log(this);
}
}
f = {
b: function() {
console.log(this);
var out = this.hey();
console.log(out);
},
hey: function() {
return "hey!";
}
}
// f.b();
/*
var func = f.b; //this gets set to the global context (which doesnt have a method b)
func();
*/
//How to manipulate context with the .call method
f = {
z: function() {
console.log(this);
}
}
// f.z.call(); //global context
// f.z.call(this);
// f.z.call(f); //context see to the f context
list = {
printNumbers: function(a,b,c) {
console.log(this);
console.log(a);
console.log(b);
console.log(c);
}
}
//pass parameters through the call method
// list.printNumbers.call(list,1,2,3);
var args = [1,2,3];
// list.printNumbers.apply(list, args);
////////////
var obj = {
hello: 'world',
f: function() {
console.log(this);
// return this.hello;
}
};
obj.f()