-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcall.js
68 lines (50 loc) · 1.71 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// function that returns product of two numbers
function product(a, b) {
return a * b;
}
// Calling product() function
const result = product(20, 5);
console.log(result); // 100
// Calling product() function with call
const result1 = product.call(20, 5);
console.log(result1); // NaN
const result2 = product.call(this, 20, 5);
console.log(result2); // 100
// ======================================================================================================= //
const person = {
fullName: function () {
return this.firstName + " " + this.lastName;
},
};
const person1 = {
firstName: "John",
lastName: "Doe",
};
const person2 = {
firstName: "Mary",
lastName: "Doe",
};
// This will return "John Doe":
const result3 = person.fullName.call(person1);
console.log(result3); // John Doe
const result4 = person.fullName.call(person2);
console.log(result4); // Mary Doe
// ======================================================================================================= //
function setUsername(username) {
this.username = username;
}
function createUser(username, email, password) {
setUsername(username);
this.email = email;
this.password = password;
}
const result5 = new createUser("Mayur144", "Mayur144@gmail.com", "Mayur144");
console.log(result5); // createUser { email: 'Mayur144@gmail.com', password: 'Mayur144' }
function createNewUser(username, email, password) {
setUsername.call(this, username);
this.email = email;
this.password = password;
}
const result6 = new createNewUser("Mayur144", "Mayur144@gmail.com", "Mayur144");
console.log(result6); // createUser { username: 'Mayur144', email: 'Mayur144@gmail.com', password: 'Mayur144' }
// https://www.youtube.com/watch?v=-owpuf4lbyU