Skip to content

Commit 2b4e2a9

Browse files
committed
Apply, Arguments
1 parent 04c8e4e commit 2b4e2a9

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

app.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
// call - runs instantly, arguments - list of items
2+
// call - runs instantly, arguments - array of items
23

34
const john = {
45
name: "john",
56
age: 25,
6-
greet: function () {
7-
console.log(this);
8-
console.log(`Hi, my name is ${this.name} and ${this.age}`);
9-
},
107
};
118

129
const susan = {
1310
name: "susan",
1411
age: 21,
1512
};
1613

17-
function greet() {
14+
function greet(city, country) {
1815
console.log(this);
19-
console.log(`Hi, my name ${this.name} and I am ${this.age} years old`);
16+
console.log(
17+
`Hi, my name ${this.name} and I am ${this.age} years old and I live in ${city}, ${country}`
18+
);
2019
}
2120

22-
// this will fail
23-
// susan.greet();
24-
// greet();
25-
const secondGreet = john.greet;
26-
// secondGreet();
21+
// greet.call(john, "san diego", "us");
22+
// greet.call(susan, "san diego", "us");
23+
// greet.call({ name: "peter", age: 30 }, "san diego", "us");
2724

28-
greet.call(john);
29-
greet.call(susan);
30-
greet.call({ name: "peter", age: 30 });
31-
32-
// Making "this" point to susan object
33-
john.greet.call(susan);
25+
greet.apply(john, ["san diego", "us"]);
26+
greet.apply(susan, ["san diego", "us"]);
27+
greet.apply({ name: "peter", age: 30 }, ["san diego", "us"]);

0 commit comments

Comments
 (0)