-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheat-sheet.html
More file actions
74 lines (65 loc) · 2.21 KB
/
cheat-sheet.html
File metadata and controls
74 lines (65 loc) · 2.21 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<!DOCTYPE html>
<head>
<title> Cheat-Sheet </title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="cheat-sheet.css">
</head>
<body>
<h1>Cheat Sheet: JavaScript Prototypes</h1>
<p>
Every JavaScript object has a prototype. The prototype is also an object. All JavaScript objects inherit their properties and methods from their prototype.
Objects created using an object literal, or with new Object(), inherit from a prototype called Object.prototype. Objects created with new Date() inherit the Date.prototype. The Object.prototype is on the top of the prototype chain. All JavaScript objects (Date, Array, RegExp, Function, ....) inherit from the Object.prototype.
</p>
<h3>Creating a Prototype</h3>
<p>
To create an object prototype is to use an object constructor function. For example:
</p>
<code>
function person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
</code>
<h3> Adding a Property to an Object</h3>
<code>
person.nationality = "English";
return "My father is " + myFather.nationality + ".
// => My father is undefined
</code>
<h3> Adding a Method to an Object</h3>
<code>
myFather.name = function () {
return this.firstName + " " + this.lastName;
};
</code>
<h3> Adding Properties to a Prototype </h3>
<code>
person.nationality = "English";
return "My father is " + myFather.nationality + ".
// => My father is undefined
</code>
<h3> Using the prototype Property </h3>
<code>
function person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
person.prototype.nationality = "English";
</code>
<h3> Using the prototype Method </h3>
<code>
function person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
person.prototype.name = function() {
return this.firstName + " " + this.lastName;
};
</code>
</body>