Skip to content

Commit c7a0b2b

Browse files
author
Genesis Gabiola
committed
End prototypes explained
1 parent f26f409 commit c7a0b2b

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* ******************************
3+
* Prototypes Explained
4+
* ******************************
5+
**/
6+
7+
// Object.prototype - Object literals all have this prototype
8+
// Person.prototype - Constructors have their own prototypes e.g. Persons.prototype but also have Object.prototype. This is known as prototype chain.
9+
10+
/**
11+
* ********************
12+
* Create a constructor
13+
* ********************
14+
**/
15+
16+
// Person constructor
17+
function Person(firstName, lastName, dob) {
18+
this.firstName = firstName;
19+
this.lastName = lastName;
20+
this.birthday = new Date(dob);
21+
// this.calculateAge = function() {
22+
// const diff = Date.now() - this.birthday.getTime();
23+
// const ageDate = new Date(diff);
24+
// return Math.abs(ageDate.getUTCFullYear() - 1970);
25+
// };
26+
}
27+
28+
/**
29+
* ********************
30+
* Create prototype methods
31+
* ********************
32+
**/
33+
// Calculate age
34+
Person.prototype.calculateAge = function() {
35+
const diff = Date.now() - this.birthday.getTime();
36+
const ageDate = new Date(diff);
37+
return Math.abs(ageDate.getUTCFullYear() - 1970);
38+
};
39+
40+
// Get full name
41+
Person.prototype.getFullName = function() {
42+
return `${this.firstName} ${this.lastName}`;
43+
};
44+
45+
// Gets married last name
46+
Person.prototype.getsMarried = function(newLastName) {
47+
this.lastName = newLastName;
48+
};
49+
50+
/**
51+
* ********************
52+
* Create object using the constructor
53+
* ********************
54+
**/
55+
const john = new Person('John', 'Doe', '8-12-90');
56+
const mary = new Person('Mary', 'Johnson', 'March 20 1978');
57+
58+
console.log(mary);
59+
// The properties firstname, lastname & dob appear for mary as properties. If we look at the __Proto__ object we can now see the different functions created above in the Person.prototype avaibale to use as seen below.
60+
61+
/**
62+
* ********************
63+
* Log prototype methods
64+
* ********************
65+
**/
66+
// Person.prototype
67+
console.log(john.calculateAge()); // 27
68+
console.log(mary.getFullName()); // Mary Johnson
69+
mary.getsMarried('Smith'); // manipulate the object property to change the last name
70+
console.log(mary.getFullName()); // Mary Smith
71+
72+
// Object.prototype
73+
console.log(mary.hasOwnProperty('firstName')); // true
74+
console.log(mary.hasOwnProperty('getFullName')); // false - this is in the prototype and not a property of its own

04-object-oriented--es5-es6/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- [Constructors & the 'this' Keyword](#constructors--the-this-keyword)
44
- [Built In Constructors](#built-in-constructors)
5+
- [Prototypes Explained](#prototypes-explained)
56

67

78
## Constructors & the 'this' Keyword
@@ -32,3 +33,14 @@ There are other core objects in JavaScript that have constructors as well; howev
3233
Primitive types such as `Strings`, `Numbers`, `Booleans` etc. can be created as constructor objects instead of primitives. There are more reason for not using constructors instead of primitives compared to the reason for using it: it slows down execution speed, more code, confusing and issues when using operators to compare values.
3334

3435
**Note**: these core object constructor exist in JavaScript but not something you will use often, learning it only to see what is possible in JavaScript for knowledge.
36+
37+
38+
## Prototypes Explained
39+
40+
- Each object in JavaScript has a prototype and a prototype is an object in itself
41+
- All object inherit their properties and methods from their prototypes
42+
- When dealing with object literals you are inheriting from a prototype called `Object.prototype`
43+
- When dealing with objects created from a constructor, it is inheriting from a prototype
44+
- There is something called the prototype chain where you have the constructor prototype but ypu can also go upto the main `Object.prototype`
45+
46+
The `Object.prototype` has its own default functions that we can call such as `hasOwnProperty()`. This will check if an object has its own property.

0 commit comments

Comments
 (0)