Last active
January 12, 2025 04:27
-
-
Save prof3ssorSt3v3/b91d1a30d3fbf376a909fb4a58b4473b to your computer and use it in GitHub Desktop.
Prototype discussion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const log = console.log; | |
function List(_values) { | |
//create a List object | |
if (!Array.isArray(_values)) return; | |
//exit the function if not given an array | |
this.items = [..._values]; //copy the array values into a property called items | |
this.size = this.items.length; | |
this.first = this.items.length > 0 ? this.items[0] : undefined; | |
this.last = this.items.length > 1 ? this.items[this.items.length - 1] : undefined; | |
Object.defineProperty(this, 'mid', { | |
get: function () { | |
return this.getMiddle(); | |
//we could put the functionality from getMiddle here instead | |
}, | |
}); | |
} | |
Object.setPrototypeOf(List.prototype, Array.prototype); //extend the prototype chain | |
// List.prototype.__proto__ = Array.prototype; //same as the above line | |
List.prototype.getMiddle = function () { | |
let middleIndex = Math.floor(this.size / 2); | |
return this.items[middleIndex]; | |
}; | |
List.prototype.map = function () { | |
//intercept map() before the map inside Array | |
return { | |
map: 'mappity, mappity mappity', | |
}; | |
}; | |
List.prototype.blah = function () { | |
return this.items.map((item) => 'blah'); | |
}; | |
let arr = new Array(1, 2, 3, 4, 5); | |
let myList = new List(arr); | |
log(myList.size, myList.first, myList.last); //access the props from this List instance | |
log(myList.getMiddle(), myList.mid); //same value twice | |
log(myList); //calls the toString() method in Object.prototype | |
log(myList instanceof List); //true | |
log(typeof myList); //object | |
log(myList.constructor.name); // List | |
log(myList.blah()); //calls the blah method in List.prototype | |
log(myList.map()); //calls the map method in List.prototype | |
log(Object.hasOwn(myList.__proto__, 'blah')); //true | |
log(Object.hasOwn(List.prototype, 'blah')); //true | |
log(Object.hasOwn(Array.prototype, 'blah')); //false | |
log(Object.getOwnPropertyNames(myList)); //first, last, mid, items, size | |
log(Object.getOwnPropertyNames(List)); // length, name, prototype | |
log(Object.getOwnPropertyNames(List.prototype)); // 'constructor', 'getMiddle', 'map', 'blah' | |
log(Object.getPrototypeOf(myList)); // 'getMiddle', 'map', 'blah' | |
log(List.prototype); // 'getMiddle', 'map', 'blah' | |
log(myList.__proto__); // 'getMiddle', 'map', 'blah' | |
log(Array.prototype); | |
List.steve = function () { | |
log('Hey Steve!'); | |
}; | |
List.steve(); //works | |
// myList.__proto__.steve(); //ERROR | |
// myList.prototype.steve(); //ERROR | |
myList.constructor.steve(); // works | |
log(Object.getOwnPropertyDescriptor(myList, 'first')); //data property | |
log(Object.getOwnPropertyDescriptor(myList, 'mid')); //accessor property |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment