mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-22 19:46:23 +00:00
917 B
917 B
date | id | title |
---|---|---|
2020-11-13 | fd03be0e-a4c0-421f-9dd7-1bddc9dece65 | JavaScript Prototypes |
Description
A prototype can be seen as an object another object extends.
Syntax
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
methods1.
console.log(Object.getPrototypeOf({}) == Object.prototype); // true
console.log(Object.getPrototypeOf(Object.prototype)); // null