
Untitled
By: a guest on
May 9th, 2012 | syntax:
None | size: 0.74 KB | hits: 14 | expires: Never
<script>
// 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');
console.log(thomas.name); // --> "Thomas"
// 2. Add a getName() method to all Person objects, that outputs
// the persons name.
Person.prototype.getName = function () {
return this.name;
}
console.log(thomas.getName()); // --> "Thomas"
// 3. Write a statement that calls Thomas's getName function,
// but returns "Amy".
//which is better call or apply?
console.log(thomas.getName.apply(amy));
console.log(thomas.getName.call(amy));
// 4. Remove the getName() method from all Person objects.
delete Person.prototype.getName;
console.log(thomas.getName);
</script>