forked from Swap76/Learn-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprototype.js
More file actions
48 lines (35 loc) · 1.2 KB
/
Copy pathprototype.js
File metadata and controls
48 lines (35 loc) · 1.2 KB
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
// Prototypes are an extension for objects in javascript
// we use to encapsulate certain functionality to an Object (like a Class)
// first create an Object Person
// that receive the name and lastName
function Person (firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
// We user the prototype keyword to add some functionality to this Person Object
// add a method getName that return the name
Person.prototype.getName = function () {
return this.firstName;
};
// add a method getLastName that return the lastName
Person.prototype.getLastName = function () {
return this.lastName;
};
// add a method getFullName that return the name + lastName
Person.prototype.getFullName = function () {
return this.firstName + " " + this.lastName;
};
// add a method getFormalName
Person.prototype.getFormalName = function () {
return this.lastName + ", " + this.firstName;
};
// these methods appear in the __proto__ property of an Object instance of Person
const max = new Person("Max", "Payne");
console.log(max.getName());
// [out] Max
console.log(max.getLastName());
// [out] Payne
console.log(max.getFullName());
// [out] Max Payne
console.log(max.getFormalName());
// [out] Payne, Max