Skip to content

Instantly share code, notes, and snippets.

@gillesruppert
Created September 30, 2010 15:19
Show Gist options
  • Save gillesruppert/604737 to your computer and use it in GitHub Desktop.
Save gillesruppert/604737 to your computer and use it in GitHub Desktop.
// 1. Write a class to support the following code:
var Person = function(name) {
this.name = name;
};
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.
Person.prototype.getName = function() {
return this.name;
};
thomas.getName(); // --> "Thomas"
// 3. Write a statement that calls Thomas's getName function,
// but returns "Amy".
// if person has no name, they will be Amy
Person.prototype.name = 'Amy';
// we could also just do it for the thomas instance by doing
// thomas.name = 'Amy';
delete thomas.name; // instance has no name anymore so we will go up the scope chain and get Amy back.
thomas.getName();
// or (without deleting thomas.name)
thomas.getName.call(amy);
// 4. Remove the getName() method from all Person objects.
delete Person.prototype.getName;
thomas.getName(); // will throw error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment