Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. // Define a simple Being prototype with isLiving(), die(), and _living starting
  2. // off as true.
  3. function Being() {}
  4. Being.prototype._living = true;
  5. Being.prototype.isLiving = function() { return this._living; };
  6. Being.prototype.die = function() { this._living = false; };
  7.  
  8. // Define a simple Person prototype which is constructed with a gender.
  9. function Person(gender) { this._gender = gender; }
  10.  
  11. // Make the Person prototype inherit from the Being prototype.
  12. Person.prototype = new Being();
  13.  
  14. // Add a getter for gender only to the Person prototype.
  15. Person.prototype.getGender = function() { return this._gender; }
  16.  
  17. // Test out a being.
  18. var being1 = new Being();
  19. console.log('Does being1 start off living?', being1.isLiving());
  20. being1.die();
  21. console.log('After executing being1.die() is being1 still living?', being1.isLiving());
  22. console.log('Does being1 have a getGender function?', 'function' == typeof being1.getGender);
  23.  
  24. // Test out a person.
  25. var person1 = new Person('Male');
  26. console.log('Does person1 start off living?', person1.isLiving());
  27. person1.die();
  28. console.log('After executing person1.die() is person1 still living?', person1.isLiving());
  29. console.log('Does person1 have a getGender function?', 'function' == typeof person1.getGender);
  30. console.log("What is person1's gender?", person1.getGender());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement