Skip to content

Commit 1fbc0c8

Browse files
committed
Constructor Function
1 parent 3c5b47a commit 1fbc0c8

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

functions/constructor-function.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,40 @@ counter1.incrementCounter();
1515
counter1.incrementCounter();
1616
counter1.incrementCounter();
1717
counter1.decrementCounter();
18+
19+
console.log("---------------------");
20+
21+
function Person() {
22+
//define properties
23+
this.name = "Joker";
24+
this.age = 25;
25+
26+
this.getName = function () {
27+
// "this" here refers to the "Person" constructor.
28+
// "this.name" is like "Person.name".
29+
return `Hello, my name is ${this.name}.`;
30+
};
31+
}
32+
33+
//Create object with Person constructor
34+
35+
const personOne = new Person();
36+
const personTwo = new Person();
37+
38+
console.log(personOne.name);
39+
console.log(personOne.getName());
40+
41+
personOne.gender = "Male";
42+
console.log(personOne.gender);
43+
console.log(personTwo.gender); // gender only exists only on personOne
44+
45+
personTwo.height = 1.8;
46+
console.log("Height:", personTwo.height);
47+
console.log("Height:", personOne.height);
48+
49+
const personThree = new Person();
50+
51+
personThree.getAge = function () {
52+
return `Age: ${this.age}`;
53+
};
54+
console.log(personThree.getAge());

0 commit comments

Comments
 (0)