-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallMethod.js
More file actions
36 lines (26 loc) · 914 Bytes
/
Copy pathcallMethod.js
File metadata and controls
36 lines (26 loc) · 914 Bytes
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
//call() It’s a predefined method in javascript.
//This method invokes a method by specifying the owner object.
function sayHello(){
return "Hello " + this.name;
}
var obj = {name: "Sandy"};
console.log(sayHello.call(obj)); // Returns "Hello Sandy"
obj = {name: "Vinod"};
console.log(sayHello.call(obj)); // Returns "Hello Vinod"
//Example with passing arguments
function saySomething(message){
return this.name + " is " + message;
}
var person4 = {name: "John"};
console.log(saySomething.call(person4, "awesome")); // Returns "John is awesome"
console.log(saySomething.call(person4, "lazy"));
//Example with
var person = {
age: 23,
getAge: function(){
return this.age;
}
}
var person2 = {age: 54};
console.log(person.getAge.call(person2)); // Returns 54
console.log(person.getAge()); // Returns 23