/**
* 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...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.