Last active
August 29, 2015 14:15
-
-
Save bIgBV/022321cdd29e72767ee7 to your computer and use it in GitHub Desktop.
This file contains 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
var Person = function(p) { | |
this.firstName = p.firstName; | |
this.lastName = p.lastName; | |
this.age = p.age; | |
this.setFirstName = function(name){ | |
if(typeof name != 'string'){ | |
throw ': NameError: Hey my should be a proper name man!'; | |
} | |
else { | |
this.firstName = name; | |
} | |
} | |
}; | |
Person.prototype = { | |
introduce : function(){ | |
console.log('Hi there, I am ' + this.firstName + ' ' + this.lastName); | |
} | |
}; | |
Person.prototype.canDrive = function(){ | |
if(this.age <= 18){ | |
console.log('Nope'); | |
} | |
else{ | |
console.log('Definitely!'); | |
} | |
}; | |
var Child = function(child){ | |
Person.call(this,{ | |
firstName: child.firstName, | |
lastName: child.lastName, | |
age : child.age | |
}); | |
}; | |
// Creating a Child.prototype object that inherits from Person | |
Child.prototype = Object.create(Person.prototype); | |
Child.prototype.constructor = Child; | |
Child.prototype.setParent = function(person){ | |
this._parent = person; | |
}; | |
Child.prototype.introduceParent = function(){ | |
console.log(this._parent.introduce()); | |
console.log(this.firstName); | |
}; | |
// Creating a new person object | |
person = { | |
firstName: 'Bhargav', | |
lastName: 'Voleti', | |
age: 22 | |
}; | |
var bhargav = new Person(person); | |
bhargav.introduce(); | |
bhargav.canDrive(); | |
bhargav.setFirstName('bIgB'); | |
bhargav.introduce(); | |
var joe = new Child({ | |
firstName: 'Joe', | |
lastName: 'Doe', | |
age: 43 | |
}); | |
joe.introduce(); | |
joe.setParent(bhargav); | |
joe.introduceParent(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment