mirror of
https://github.com/alrayyes/wiki.git
synced 2025-07-24 01:43:26 +00:00
Quartz sync: May 6, 2024, 10:40 PM
This commit is contained in:
parent
aee9145691
commit
4ef8371441
635 changed files with 22281 additions and 6 deletions
37
content/20201113091424-javascript_prototypes.md
Normal file
37
content/20201113091424-javascript_prototypes.md
Normal file
|
@ -0,0 +1,37 @@
|
|||
---
|
||||
id: fd03be0e-a4c0-421f-9dd7-1bddc9dece65
|
||||
title: JavaScript Prototypes
|
||||
---
|
||||
|
||||
# Description
|
||||
|
||||
A prototype can be seen as an [object](20200826201605-objects) another
|
||||
object extends.
|
||||
|
||||
# Syntax
|
||||
|
||||
``` javascript
|
||||
let protoRabbit = {
|
||||
speak(line) {
|
||||
console.log(`The ${this.type} rabbit says '${line}'`); // : The killer rabbit says 'SKREEEE!'
|
||||
},
|
||||
};
|
||||
let killerRabbit = Object.create(protoRabbit);
|
||||
killerRabbit.type = "killer";
|
||||
killerRabbit.speak("SKREEEE!");
|
||||
```
|
||||
|
||||
# Object.prototype
|
||||
|
||||
Most objects in JavaScript eventually extend `Object.prototype` through
|
||||
parent prototype objects or directly, which provides a bunch of default
|
||||
methods[^1].
|
||||
|
||||
``` javascript
|
||||
console.log(Object.getPrototypeOf({}) == Object.prototype); // true
|
||||
console.log(Object.getPrototypeOf(Object.prototype)); // null
|
||||
```
|
||||
|
||||
# Footnotes
|
||||
|
||||
[^1]: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object>
|
Loading…
Add table
Add a link
Reference in a new issue