-
Notifications
You must be signed in to change notification settings - Fork 0
/
call.js
44 lines (35 loc) · 1.48 KB
/
call.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
'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.call(obj, 6, 7);
/*
* ----------------------------------------------------------------------------------------------------
* The call() method can be used to invoke (call) a method with an owner object as an argument (parameter).
* The call() method takes arguments separately.
* ----------------------------------------------------------------------------------------------------
*/
/*
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 call() method can be used to invoke (call) a method with an owner object as an argument (parameter).
The call() method takes arguments separately.
-------------------------------------------------------------------------------------------------------
The difference is:
The call() method takes arguments separately.
The apply() method takes arguments as an array.
*/