ZAKAS OOP CHAPTER FOUR : constructors / prototypes

Notes and demo from Zakas Book

by nickadeemus2002

HTML

<script src="https://getfirebug.com/firebug-lite-debug.js"></script>

JavaScript

/*
*******************************************************
ZAKAS OOP CHAPTER FOUR : constructors / prototypes
*******************************************************
*/


/*
**********************************************************
prototypes ... PROTOTYPE OBJECTS is where the fun is at
--they are recipes for objects and each instance keeps
track of it's origin using [[PROTOTYPE]] internal
**********************************************************
*/



//when using object prototype methods, JS will look for 
//the matching method on the instance, if not there, it
//will move up to the next objeect in the prototype chain,
//if not there, it will keep going until it finds it

var genericObject = {};

//Object.toString used
console.log(genericObject.toString());

//we want better info about the INSTANCE, so
//overwrite default toString with ownProperty method
//with same name
genericObject.toString=function(){
    return '[object GenericObject]';
};

//run toString on genericObject
console.log(genericObject.toString());

//now remove it to use Object Prototype again
delete genericObject.toString;

//Object.toString used again
console.log(genericObject.toString());



/*
// For any generic object, [[Prototype]] is always a reference 
// to Object.prototype.
var object = {};
var prototype = Object.getPrototypeOf(object);
//true
console.log(prototype === Object.prototype);

// TEST to see if one object is a prototype for 
// another by using the isPrototypeOf() method:

var object = {};
//true
console.log(Object.prototype.isPrototypeOf(object)); 


//******************************************************************************************


// An INSTANCE keeps track of its prototype through an internal 
//property called [[Prototype]]. This property is a pointer back 
//to the prototype object that the instance is using. When you create
//a new object using ~~~ new ~~~, the constructor’s prototype 
//property is assigned to the [[Prototype]] property of that new...