-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply.js
More file actions
44 lines (35 loc) · 1.48 KB
/
Copy pathapply.js
File metadata and controls
44 lines (35 loc) · 1.48 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
'use strict'
const obj = {
a:1,
b:2,
c: function(arg1, arg2) {
console.log(`The given values are ${this.a} and ${this.b}. The arguments are ${arg1} and ${arg2}` )
}
}
// obj.c(9,5)
// const x = obj.c;
// x(6, 7);
const x = obj.c;
x.apply(obj, [6, 7]);
/*
* ----------------------------------------------------------------------------------------------------
* The apply() method can be used to invoke (call) a method with an owner object as an argument (parameter).
* The apply() method takes arguments as an array.
* ----------------------------------------------------------------------------------------------------
*/
/*
The this keyword refers to different objects depending on how it is used:
-In an object method, this refers to the object.
-Alone, this refers to the global object.
-In a function, this refers to the global object.
-In a function, in strict mode, this is undefined.
-In an event, this refers to the element that received the event.
-Methods like call(), apply(), and bind() can refer this to any object.
----------------------------------------------------------------------------------------------------
The apply() method can be used to invoke (call) a method with an owner object as an argument (parameter).
The apply() method takes arguments as an array.
----------------------------------------------------------------------------------------------------
The difference is:
The call() method takes arguments separately.
The apply() method takes arguments as an array.
*/