Prototypes
Video
JavaScript Notes
JavaScript
/******************************************************
// [ toString, valueOf, isPrototypeOf hasOwnProperty……]
// obj ==> obj.prototype (Object prototype) ==> null
Object prototypes in JavaScript
******************************************************/
let obj1 = {
prop1: ( ) => { console.log('prop1') }
} ;
let obj2 = {
prop2: ( ) => { console.log('prop2') }
} ;
obj.prop1( ); // returns ‘prop1’
obj.prop2( ); // will check the prototype chain for ‘prop2’ otherwise will return an error
obj.toString( ); // every object in JavaScript has a prototype object. That object has the toString Method
Object.setPrototypeOf(obj2, obj1); // set the prototype of obj2 to be obj1
obj2.prop2( );
obj2.prop1( );
for (let prop in obj2) {
console.log(prop); // logs out each property in obj2
}
Object.getPrototypeOf(obj2) // returns the prototype of obj2, which is obj1. Same as just writing obj1
Object.getPrototypeOf(obj2).prop1( ) // returns ‘prop1’
Object.getOwnPropertyNames(obj2); // returns all properties in just obj2
Object.getOwnPropertyNames(Object.PrototypeOf(obj1)); // returns all properties of default object
Object.create( ) // can create custom properties, with property descriptors
Object.assign( ) // can just list of the properties
Feedback
Submit and view feedback