Skip to content

Instantly share code, notes, and snippets.

@codekipple
Created March 4, 2011 14:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codekipple/854673 to your computer and use it in GitHub Desktop.
Save codekipple/854673 to your computer and use it in GitHub Desktop.
javascript master class with Amy Hoy and Thomas Fuchs: Homework part 1
// 1. Write a class to support the following code:
// var thomas = new Person('Thomas');
// var amy = new Person('Amy');
// thomas.name // --> "Thomas"
var Person = function( name ){
this.name = name;
};
var thomas = new Person('Thomas');
var amy = new Person('Amy');
console.log( thomas.name ) // --> "Thomas"
// 2. Add a getName() method to all Person objects, that outputs the persons name.
// thomas.getName() // --> "Thomas"
var Person = function( name ){
this.name = name;
};
Person.prototype.getName = function(){
console.log( this.name );
};
var thomas = new Person('Thomas');
thomas.getName(); // --> "Thomas"
// 3. Write a statement that calls Thomas's getName function, but returns "Amy".
thomas.getName.call(amy); // --> "Amy"
// 4. Remove the getName() method from all Person objects.
delete Person.prototype.getName;
thomas.getName(); // --> "thomas.getName is not a function"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment