Created
January 21, 2011 15:13
-
-
Save themartorana/789806 to your computer and use it in GitHub Desktop.
Inheritance Homework
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
// 1. Write a class to support the following code: | |
// var thomas = new Person('Thomas'); | |
// var amy = new Person('Amy'); | |
// | |
// thomas.name // --> "Thomas" | |
// 2. Add a getName() method to all Person objects, that outputs | |
// the persons name. | |
//..thomas.getName() // --> "Thomas" | |
// 3. Write a statement that calls Thomas's getName function, | |
// but returns "Amy". | |
// 4. Remove the getName() method from all Person objects. | |
var Person = function(name) { | |
this.name = name; | |
}; | |
Person.prototype.name = 'Amy'; | |
Person.prototype.getName = function() { | |
return this.name; | |
}; | |
var thomas = new Person('Thomas'); | |
var amy = new Person('Amy'); | |
console.log(thomas.name); | |
console.log(amy.name); | |
console.log(thomas.getName()); | |
console.log(amy.getName()); | |
thomas.getName = function() { | |
return this.constructor.prototype.name; | |
}; | |
console.log(thomas.getName()); | |
/* Before delete getName */ | |
console.log('getName' in amy); | |
console.log('getName' in thomas); | |
console.log('getName' in Person.prototype); | |
var people = [amy, thomas]; | |
for (i = 0; i < people.length; i++) { | |
var person = people[i]; | |
if (person.hasOwnProperty('getName')) { | |
delete person.getName; | |
} | |
if ('getName' in person) { | |
delete person.constructor.prototype.getName; | |
} | |
} | |
/* After delete getName */ | |
console.log('getName' in amy); | |
console.log('getName' in thomas); | |
console.log('getName' in Person.prototype); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment