Skip to content

Commit 5406461

Browse files
Added Object Class
1 parent cd90ea9 commit 5406461

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Object_Class.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Person {
2+
constructor(name, age) {
3+
this.name = name;
4+
this.age = age;
5+
}
6+
7+
// Built-in, returns a string by default
8+
toString() {
9+
return `This person's name is ${this.name}, ${this.age} years old.`;
10+
}
11+
12+
// Custom function, must be specified in order to call
13+
Greet() {
14+
return `Hello there ${this.name}.`;
15+
}
16+
}
17+
18+
19+
var person1 = new Person("John", 17);
20+
console.log(person1.toString());
21+
22+
var person2 = new Person("Jake", 19);
23+
console.log(person2.toString());
24+
25+
var person3 = new Person("Blake", 22);
26+
var person3Greeting = person3.Greet(); // Calling the function
27+
console.log(person3Greeting); // Calling the variable
28+

0 commit comments

Comments
 (0)