Created
September 3, 2010 08:53
-
-
Save girish/563635 to your computer and use it in GitHub Desktop.
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
/* Javascript Object Model */ | |
/*Please Note that print statements don't exactly print what i say in | |
comments, they are simplified for simplicity :). | |
But the idea remains same. | |
*/ | |
//obj is now an object Phew!! | |
var obj={} | |
//add a property | |
obj.x=1 | |
//add a property that is an object | |
obj.y={} | |
// add a method | |
obj.z=function(){ | |
} | |
//jbo is also object. | |
//Guess what Object is? function(is a constructor,remember?) | |
var jbo=new Object() | |
//add properties | |
jbo.x=1 | |
//add a property that is an object | |
jbo.y={} | |
//Both prints Object,we constructed using Object function | |
// {} is nothing but a syntatic sugar | |
console.log(jbo.constructor) | |
console.log(obj.constructor) | |
//custom contructor | |
function Boy(){ | |
//add property on object when created | |
//properties specfic to an object | |
this.city='Hyderabad' | |
} | |
//add to prototype generally all methods (also properties) are added here, | |
//Because they are common to every object(we will see why) | |
Boy.prototype.gender='male' | |
var iam=new Boy() | |
//prints Hyderabad | |
console.log(iam.city) | |
//prints Boy() | |
console.log(iam.constructor) | |
//can continue to add properties specfic to this object | |
iam.isGreat=true | |
//prints Function(), Function is a constructor for function objects | |
//implies Function is also a function | |
console.log(Boy.constructor) | |
//our object's proto references constructor's prototype | |
//prints true | |
console.log(iam.__proto__ === Boy.prototype) | |
//PROPERTY LOOKUPS | |
//Remember how property lookups happens. if not present in object, | |
//it searches in where it's proto references ie Boy.prototype here | |
//Now it makes sense to add common properties in | |
//contructor's prototype, isn't it? its like added methods on class | |
//in classical inheritance languages | |
//Prints male | |
console.log(iam.gender) | |
iam.gender='m' | |
//prints m | |
//as property can be found in the object it doesn't use proto | |
console.log(iam.gender) | |
//prints '[object Object]' | |
//where did it come from? | |
//from Object continue reading... | |
console.log(iam.toString()) | |
//Boy.prototype is an object, so what about its proto | |
//print Object.prototype | |
//it has toString() and other properties defined. | |
//Now u know where it came from | |
console.log(Boy.prototype.__proto__) | |
//few more examples | |
//Following are true | |
//jbo.__proto__ === Object.prototype | |
//obj.__proto__ === Object.prototype | |
//Object.prototype is an object, so what about its proto | |
//it's null. it's the end for proto' | |
//print null | |
console.log(Object.prototype.__proto__) | |
//Recap | |
//iam.__proto__ references Boy.prototype (its constructor) | |
//Boy.prototype.__proto__ references Object.prototype(Boy.protoype plain object like obj) | |
//Object.prototype.__proto__ references null(end of chain) | |
//INHERITANCE IN JAVASCRIPT | |
function GoodBoy(){ | |
this.good = true; | |
} | |
// GoodBoy is a Boy much before becoming good,isn't it? | |
// Well, we can rewrite all the proterties of a boy in GoodBoy. | |
// oops!!, that not OOPS | |
// so How to make him behave like a boy,Inheritance?Kidding me,for a function? | |
// Our constructor is function and ofcourse an object. | |
// Property look up chain to rescue | |
//prints Object | |
//we need to break the chain and insert our Boy between GoodBoy and Object | |
console.log(GoodBoy.prototype.__proto__) | |
//Our prototype objects constructor is ... | |
//Prints true, | |
//Yes its ... | |
GoodBoy.prototype.constructor === GoodBoy | |
GoodBoy.prototype=new Boy() | |
//Now a Bad News, I lied to you | |
//Good News, Its just a white lie | |
//I said constructor property is defined on every object | |
//its only defined on prototype's of constructor function | |
//Eg. Boy.prototype , Object.prototype | |
//Every objeccts proto referecenes atleast one prototype object | |
//which means it's valid to say every object has one, or is it? | |
//iam.constructor is actucally iam.__proto__.constructor | |
//ie Boy.prototype.constructor | |
//As the GoodBoy.prototype is now referencing new Boy object | |
//All previous properties, including constructor are last | |
//So GoodBoy.prototype.constructor is now GoodBoy.prototype.__proto__.constructor | |
//ie Boy.prototype.constructor ie Boy | |
// it should point to GoodBoy, because its GoodBoy's prototype | |
// lets fix it | |
GoodBoy.prototype.constructor = GoodBoy | |
//Haa we are not done, Its much more complex, what about initializing | |
//super class. This is just an introduction to javascript object model |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment