-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject-based.js
39 lines (30 loc) · 1.17 KB
/
object-based.js
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
32
33
34
35
36
37
38
39
// define a person object
function person(first, last, age, gender, interests) {
this.name = {first, last};
this.age = age;
this.gender = gender;
this.interests = interests;
this.bio = function () {
console.log(this.name.first + ' ' + this.name.last + ' is ' + this.age + ' years old. They like ' + this.interests);
};
this.greeting = function() {
console.log('Hi! I\'m ' + this.name.first + '.');
};
}
// create an instance
var person1 = new person('Henry', 'Smith', 21, 'male', 'Music, Photography, Awesomeness');
// output the persons name (use a member)
// output the persons name (use a member)
person1.greeting();
// output the greeting method that we created
person1.bio();
// try to call something that does not exist yet, a method called farewell
////console.log("this next call will fail");
//person1.farewell();
console.log("adding new method, farewell to prototype");
// now add to the prototype of the object the farewell method
person.prototype.farewell = function() {
console.log(this.name.first + ' has left the builing!');
};
// rerun the farewell method you just created on the object that was instantiated before the farewell method existed
person1.farewell();