Skip to content

Instantly share code, notes, and snippets.

@themartorana
Created January 21, 2011 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save themartorana/789806 to your computer and use it in GitHub Desktop.
Save themartorana/789806 to your computer and use it in GitHub Desktop.
Inheritance Homework
// 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