Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaScript Prototypal Inheritance
function ParentScope(){
this.aString = "parent string";
this.aNumber = 100;
this.anArray = [10,20,30];
this.anObject = {'property1': 'parent prop1', 'property2': 'parent prop2' };
this.aFunction = function(){ console.log('parent output'); }
}
function ChildScope(){
}
ChildScope.prototype = new ParentScope();
var childScope = new ChildScope();
//#########################1
console.log( "####################### Example 1" );
console.log( childScope.aString ); //not in child, prototype chain followed... parent's property found.
childScope.aString = 'child string'; //when doing a primitive like this, it creates a new property on child object (vs if thought it would update the parent object's aString property)
console.log( childScope.aString );
//#########################2
console.log( "####################### Example 2" );
console.log( childScope.anArray[1] ); //updates the parent object's property
console.log( childScope.anObject.property1 );//updates the parent object's property
childScope.anArray[1] = 22;
childScope.anObject.property1 = 'child prop1';
console.log( childScope.anArray[1] );
console.log( childScope.anObject.property1 );
//#########################3
console.log( "####################### Example 3" );
console.dir( childScope.anArray );
console.dir( childScope.anObject );
childScope.anArray = [100, 555]; //creates a new object on child object
childScope.anObject = { name: 'Mark', country: 'USA' };
console.dir( childScope.anArray );
console.dir( childScope.anObject );
//#########################4
console.log( "####################### Example 4" );
delete childScope.anArray; //child's property deleted, parent's property still remains and 'shines' through though
console.log(childScope.anArray[1] === 22); // true, parent's property looked at
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.