-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontext.js
More file actions
47 lines (41 loc) · 1.44 KB
/
context.js
File metadata and controls
47 lines (41 loc) · 1.44 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
// This is some function.
function someFunction() {
console.log("Some function.", this);
}
// This is some object.
let someObject = {
name: "someObject",
propOne: "Property One",
propTwo: 1,
funcOne: someFunction,
funcOneWindowOne: someFunction.bind(window),
funcOneWindowTwo: someFunction.bind(this),
logFunc: function() {
console.group(`${this.name} log.`)
console.log(`PropOne: ${this.propOne}`);
console.log(`PropTwo: ${this.propTwo}`);
console.groupEnd();
},
withParamsFunc: function(firstParam, secondParam) {
console.group(`${this.name} log.`)
console.log(`First param: ${firstParam}`);
console.log(`Second param: ${secondParam}`);
console.groupEnd();
}
}
// This is another object.
let anotherObject = {
name: "anotherObject",
propOne: "Property Two",
propTwo: 2
}
// This method (bind) call function in some context.
const fnAnotherObjectLog = someObject.logFunc.bind(anotherObject);
const fnAnotherObjectWithParams = someObject.withParamsFunc.bind(anotherObject);
fnAnotherObjectLog();
fnAnotherObjectWithParams("p1", "p2");
someObject.withParamsFunc.bind(anotherObject, "p1", "p2")();
// This method (call) immediately calls function.
someObject.withParamsFunc.call(anotherObject, "p1", "p2");
// This method (apply) immediately calls function, but have only two params.
someObject.withParamsFunc.apply(anotherObject, ["p1", "p2"]);