-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcall.js
More file actions
31 lines (26 loc) · 677 Bytes
/
call.js
File metadata and controls
31 lines (26 loc) · 677 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
/**
* Basic Example
*/
function Product(name,price){
this.name=name;
this.price=price;
}
function Food(name,price){
Product.call(this,name,price);
this.category="food";
}
let newFood=new Food('Cheese',5);
console.log(`newFood: ${JSON.stringify(newFood,2,null)}`);
// Output : newFood: {"name":"Cheese","price":5,"category":"food"}
/**
* Using call() to invoke a function and specifying the this value
*/
function greet() {
console.log(this.animal, "typically sleep between", this.sleepDuration);
}
const inputObject = {
animal: "cats",
sleepDuration: "12 and 16 hours",
};
greet.call(inputObject);
// cats typically sleep between 12 and 16 hours