JSFiddle - React, Tailwind, and code Playground

OOP-: Ch5- Inheritance

by nickadeemus2002

JavaScript

/**
 * OOP: ch.5 - Inheritance
 * =====================================================
 * Mechanism for inheritance is [[Prototype].
 * JavaScript’s built-in approach for inheritance is 
 *	called prototype chaining, or prototypal inheritance.
 */
 


/*
* Prototype Chaining and Object.prototype:
* =========================================
* The object instances inherit properties from
*	the prototype. Because the prototype is also 
*	an object, it has its own prototype and inherits
*	properties from that. 
*
*	This is the prototype chain: An object inherits 
*	from its prototype, while that prototype in 
*	turn inherits from its prototype, and so on.
*/


/*
* Object literal prototype:
* =========================================
*	myBook automatically receives methods from Object.prototype.
* 
*	hasOwnProperty():
*	determines whether an own property with the given 
*	name exists
*
* propertyIsEnumerble():
*	etermines whether an own property is enumerable
*
* isPrototypeOf():
*	determines whether the object is the prototype of another
*
*	valueOf():
*	returns the value representation of the object
*
*	toString():
*	returns a string representation of the object
*/


var myBook = {
	title: "Object-Oriented JavaScript Training",
  //customize toString to be more helpful
	toString: function() {
		return "[Book " + this.title + "]"
	}
};
var prototype = Object.getPrototypeOf(myBook);
console.log('prototype === Object.prototype: ', prototype === Object.prototype); 		// true
console.log('"hasOwnProperty" in myBook: ', "hasOwnProperty" in myBook);
console.log('"propertyIsEnumerable" in myBook: ', "propertyIsEnumerable" in myBook);
console.log('"isPrototypeOf" in myBook: ', "isPrototypeOf" in myBook);
console.log('"valueOf" in myBook: ', "valueOf" in myBook)
console.log('"toString" in myBook: ', "toString" in myBook)

//customize toString to be more helpful
var describeMyBook = "Descriptor : " + myBook;
// "Book =  [Object-Oriented JavaScript...