-
Notifications
You must be signed in to change notification settings - Fork 10
/
inheritanceUsingObjectConstructors.js
executable file
·62 lines (53 loc) · 2.06 KB
/
inheritanceUsingObjectConstructors.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* jshint proto: true */
(function() {
"use strict";
var exampleTitle = "Inheritance using object constructors - ";
var originalClient = {
"id": 1,
"name": "Baxter Brooks",
"gender": "male",
"type": "client",
"email": "[email protected]",
"registered": "2014-03-15T10:52:05 -00:00",
"isActive": false
};
function Contact(id, name, gender, email) {
this.id = id;
this.name = name;
this.gender = gender;
this.type = "contact";
this.email = email;
}
var contact = new Contact(
1,
"Baxter Brooks",
"male",
);
console.log(exampleTitle + "Assert that [[Prototype]] for 'contact' instance is the object constructor prototype: " + (contact.__proto__ === Contact.prototype));
var assertContactIsInstanceOfContact = contact instanceof Contact;
console.log(exampleTitle + "Assert that 'contact' is an instance of Contact: " + assertContactIsInstanceOfContact);
function Client(id, name, gender, email, registered, isActive) {
Contact.call(this, id, name, gender, email);
this.type = 'client';
this.registered = registered;
this.isActive = isActive;
}
Client.prototype = Object.create(Contact.prototype);
Client.prototype.constructor = Client;
var client = new Client(
1,
"Baxter Brooks",
"male",
"2014-03-15T10:52:05 -00:00",
false
);
console.log(exampleTitle + "Assert that [[Prototype]] for 'client' inherits from the [[Prototype]] for 'contact': " + (client.__proto__.__proto__ === contact.__proto__));
var assertClientIsInstanceOfContact = client instanceof Contact;
console.log(exampleTitle + "Assert that 'client' is an instance of Contact: " + assertClientIsInstanceOfContact);
Contact.prototype.getContactIdAndName = function() {
return this.name + " (" + this.id + ")";
};
console.log(exampleTitle + "Assert that all objects inheriting from 'Contact' or that are 'Contact' have the same value for getContactIdAndName(): " + (contact.getContactIdAndName() === client.getContactIdAndName()));
}());