Skip to content

Commit a4c0e0a

Browse files
committed
created function constructor
1 parent 8e69c63 commit a4c0e0a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

5-advanced-JS/starter/script.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
//function constructors
3+
var Person = function(name,yearOfBirth, job){
4+
this.name = name;
5+
this.yearOfBirth = yearOfBirth;
6+
this.job = job;
7+
}
8+
//Using the prototype method to include function in constructor
9+
Person.prototype.calculateAge = function(){
10+
console.log(this.name, 'Age: ', 2016 - this.yearOfBirth);
11+
}
12+
13+
Person.prototype.lastName = 'Smith';
14+
15+
// Instanciation
16+
var john = new Person('John', 1990, 'Teacher');
17+
var jane = new Person('Jane', 1969, 'designer');
18+
var mark = new Person('Mark', 1948, 'retired');
19+
20+
john.calculateAge();
21+
jane.calculateAge();
22+
mark.calculateAge();
23+
24+
console.log(john.lastName);
25+
console.log(jane.lastName);
26+
console.log(mark.lastName);

0 commit comments

Comments
 (0)