Advertisement
Guest User

Untitled

a guest
May 6th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. //prototypal inheritance
  2. var human = {
  3. species: "human",
  4. create: function(values) {
  5. var instance = Object.create(this);
  6. Object.keys(values).forEach(function(key) {
  7. instance[key] = values[key];
  8. });
  9. return instance;
  10. },
  11. saySpecies: function() {
  12. console.log(this.species);
  13. },
  14. sayName: function() {
  15. console.log(this.name);
  16. }
  17. };
  18.  
  19. var musician = human.create({
  20. species: "musician",
  21. playInstrument: function() {
  22. console.log("plays " + this.instrument);
  23. }
  24. });
  25.  
  26. var will = musician.create({
  27. name: "Will",
  28. instrument: "drums"
  29. });
  30.  
  31. will.playInstrument(); // plays drums
  32. will.sayName(); //will
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement