Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // this is wrong because all Cat instances would inherit from a single instance of Animal
- // which means that any members that were initialized in the Animal constructor
- // would be inherited, possibly overriding what was in Animal.prototype.
- // also, the constructor property if Cat instances would point to the wrong constructor.
- function Animal(){}
- function Cat(){}
- Cat.prototype = new Animal();
- var myAnimal = new Animal();
- var myCat = new Cat();
- WScript.Echo((myCat instanceof Cat).toString() + ":true");
- WScript.Echo((myCat instanceof Animal).toString() + ":true");
- WScript.Echo((myCat.constructor === Cat).toString()+ ":true");
- WScript.Echo((myCat.constructor === Animal).toString() + ":false");
- WScript.Echo((myAnimal instanceof Cat).toString() + ":false");
- WScript.Echo((myAnimal instanceof Animal).toString() + ":true");
- WScript.Echo((myAnimal.constructor === Animal).toString() + ":true");
- WScript.Echo((myAnimal.constructor === Cat).toString() + ":false");
- WScript.Echo("---------------------------------------")
- WScript.Echo("---------------------------------------")
- //this is wrong because both Cat and Animal refer to the exact same object
- // this means that myAnimal is now both an instanceof Cat and an instanceof Animal
- // also, myCat.constructor is Animal and not Cat
- // and if I need to update the behavior of all Cats, I can't do so without also changing the
- //behavior of all Animals as well since they both share the exact same object
- function Animal(){}
- Animal.prototype.foo = function(){
- return "Hola"
- }
- function Cat(){}
- Cat.prototype = Animal.prototype;
- var myAnimal = new Animal()
- var myCat = new Cat();
- WScript.Echo((myCat instanceof Cat).toString() + ":true");
- WScript.Echo((myCat instanceof Animal).toString() + ":true");
- WScript.Echo((myCat.constructor === Cat).toString()+ ":true");
- WScript.Echo((myCat.constructor === Animal).toString() + ":false");
- WScript.Echo("---------------------------------------")
- WScript.Echo((myAnimal instanceof Cat).toString() + ":false");
- WScript.Echo((myAnimal instanceof Animal).toString() + ":true");
- WScript.Echo((myAnimal.constructor === Animal).toString() + ":true");
- WScript.Echo((myAnimal.constructor === Cat).toString() + ":false");
- WScript.Echo(myAnimal.foo() + ":Hola")
- WScript.Echo(myCat.foo() + ":Hola")
- Cat.prototype.foo = function(){
- return "Bueno"
- }
- WScript.Echo(myAnimal.foo() + ":Hola")
- WScript.Echo(myCat.foo() + ":Bueno")
- WScript.Echo("---------------------------------------")
- WScript.Echo("---------------------------------------")
- //The correct way
- function Animal(){}
- function Cat(){}
- Cat.prototype = copy(Animal.prototype)
- extend(Cat.prototype, {
- constructor : Cat,
- myExtendedMember : function(){
- return "foo"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment