mlhaufe

Inheritance

Jun 2nd, 2011
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // this is wrong because all Cat instances would inherit from a single instance of Animal
  2. // which means that any members that were initialized in the Animal constructor
  3. // would be inherited, possibly overriding what was in Animal.prototype.
  4. // also, the constructor property if Cat instances would point to the wrong constructor.
  5. function Animal(){}
  6. function Cat(){}
  7. Cat.prototype = new Animal();
  8.  
  9. var myAnimal = new Animal();
  10. var myCat = new Cat();
  11.  
  12. WScript.Echo((myCat instanceof Cat).toString() +     ":true");
  13. WScript.Echo((myCat instanceof Animal).toString() +  ":true");
  14. WScript.Echo((myCat.constructor === Cat).toString()+ ":true");
  15. WScript.Echo((myCat.constructor === Animal).toString() + ":false");
  16. WScript.Echo((myAnimal instanceof Cat).toString() + ":false");
  17. WScript.Echo((myAnimal instanceof Animal).toString() + ":true");
  18. WScript.Echo((myAnimal.constructor === Animal).toString() + ":true");
  19. WScript.Echo((myAnimal.constructor === Cat).toString() + ":false");
  20.  
  21. WScript.Echo("---------------------------------------")
  22. WScript.Echo("---------------------------------------")
  23.  
  24.  
  25. //this is wrong because both Cat and Animal refer to the exact same object
  26. // this means that myAnimal is now both an instanceof Cat and an instanceof Animal
  27. // also, myCat.constructor is Animal and not Cat
  28. // and if I need to update the behavior of all Cats, I can't do so without also changing the
  29. //behavior of all Animals as well since they both share the exact same object
  30.  
  31. function Animal(){}
  32. Animal.prototype.foo = function(){
  33.     return "Hola"
  34. }
  35. function Cat(){}
  36. Cat.prototype = Animal.prototype;
  37.  
  38. var myAnimal = new Animal()
  39. var myCat = new Cat();
  40.  
  41. WScript.Echo((myCat instanceof Cat).toString() +     ":true");
  42. WScript.Echo((myCat instanceof Animal).toString() +  ":true");
  43. WScript.Echo((myCat.constructor === Cat).toString()+ ":true");
  44. WScript.Echo((myCat.constructor === Animal).toString() + ":false");
  45. WScript.Echo("---------------------------------------")
  46. WScript.Echo((myAnimal instanceof Cat).toString() + ":false");
  47. WScript.Echo((myAnimal instanceof Animal).toString() + ":true");
  48. WScript.Echo((myAnimal.constructor === Animal).toString() + ":true");
  49. WScript.Echo((myAnimal.constructor === Cat).toString() + ":false");
  50. WScript.Echo(myAnimal.foo() + ":Hola")
  51. WScript.Echo(myCat.foo() + ":Hola")
  52. Cat.prototype.foo = function(){
  53.     return "Bueno"
  54. }
  55. WScript.Echo(myAnimal.foo() + ":Hola")
  56. WScript.Echo(myCat.foo() + ":Bueno")
  57.  
  58.  
  59. WScript.Echo("---------------------------------------")
  60. WScript.Echo("---------------------------------------")
  61.  
  62. //The correct way
  63.  
  64. function Animal(){}
  65. function Cat(){}
  66. Cat.prototype = copy(Animal.prototype)
  67. extend(Cat.prototype, {
  68.     constructor : Cat,
  69.     myExtendedMember : function(){
  70.         return "foo"
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment