Skip to content

Commit 64147ca

Browse files
committed
Section 19: What on Earth are Prototypes
1 parent 26bedd2 commit 64147ca

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// ******************************
2+
// What on Earth are Prototypes ******************************
3+
4+
console.log('What on Earth are Prototypes')
5+
console.log('\n')
6+
7+
// Example 1
8+
String.prototype.grumpus = () => {
9+
console.log('GO AWAY!!!')
10+
}
11+
12+
console.log(String.prototype)
13+
14+
console.log('\n');
15+
console.log(String.prototype.grumpus)
16+
17+
console.log('\n')
18+
const cat = 'Blue'
19+
console.log(cat.grumpus());
20+
21+
console.log('\n')
22+
// Example 2
23+
String.prototype.yell = function() {
24+
console.log(this);
25+
// This refers to whatever string we're calling the method on
26+
return `OMG!!! ${this.toUpperCase()}!!!!!! AHGHGHGHGGHH!`
27+
}
28+
29+
const hello = 'Hello'
30+
console.log(hello.yell())
31+
32+
console.log('\n')
33+
// Example 3
34+
// Overwritting .pop
35+
Array.prototype.pop = function() {
36+
return 'SORRY I WANT THAT ELEMENT, I WILL NEVER POP IT OFF!'
37+
}
38+
39+
const arr = [1, 2, 3, 4, 5]
40+
console.log(arr.pop())
41+
console.log('\n')
42+
console.log(arr);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>JavaScript</title>
8+
</head>
9+
<body>
10+
<script src="app.js"></script>
11+
</body>
12+
</html>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## 19-Prototypes-Classes-And-The-New-Operator
2+
3+
## 1. What on Earth are Prototypes
4+
5+
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_prototypes
6+
7+
**__proto__**, So rather than having a separate method on every single array called push, there is one prototype and each array has a reference to that prototype with this special property **__proto__**. And we actually can see this in other types of objects in JavaScript.
8+
9+
```javascript
10+
console.log(Array.prototype)
11+
console.log(Object.prototype)
12+
console.log(String.prototype)
13+
```
14+
15+
I could actually define my own type of an object and set its prototype to the array prototype. And i would have access to those array methods in my whatever object, my version of the array.
16+
17+
Prototypes are lik e a template object. They are objects, they contain typically a bunch of methods, and then we can create multiple objects that share the same prototype so that they all have access to the same methods without having to have their own copy.

0 commit comments

Comments
 (0)