Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. SuperHero.prototype = Object.create( Person.prototype );
  2.  
  3. var Person = function( firstName , lastName ){
  4. this.firstName = firstName;
  5. this.lastName = lastName;
  6. this.gender = "male";
  7. };
  8.  
  9. // a new instance of Person can then easily be created as follows:
  10. var clark = new Person( "Clark" , "Kent" );
  11.  
  12. // Define a subclass constructor for for "Superhero":
  13. var Superhero = function( firstName, lastName , powers ){
  14.  
  15. // Invoke the superclass constructor on the new object
  16. // then use .call() to invoke the constructor as a method of
  17. // the object to be initialized.
  18.  
  19. Person.call( this, firstName, lastName );
  20.  
  21. // Finally, store their powers, a new array of traits not found in a normal "Person"
  22. this.powers = powers;
  23. };
  24.  
  25. SuperHero.prototype = Object.create( Person.prototype );
  26. var superman = new Superhero( "Clark" ,"Kent" , ["flight","heat-vision"] );
  27. console.log( superman );
  28.  
  29. // Outputs Person attributes as well as powers
  30.  
  31. SuperHero.prototype = new Person();
  32.  
  33. SuperHero.prototype = Object.create( Person.prototype );
  34.  
  35. SuperHero.prototype = new Person();
  36.  
  37. var person = new Person();
  38. SuperHero.prototype = person; // inheritance here
  39. // the Object.create way is ECMAScript 5 (which not all browsers support yet sadly)
  40.  
  41. var superman = new SuperHero( "Clark" ,"Kent" , ["flight","heat-vision"] );
  42. console.log( person );
  43. console.log( superman ); ​
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement