0
      <html>
      <body>
      <script>    
      function animal(kane){
      this.kane:"aaaaa";
      }
      function Rabbit(name) {
      this.name = name;
      } 
      Rabbit.prototype.__proto__=animal.prototype;
      var a=new animal("aaaa");// this wont work when i put a alert box
      var rabbit = new Rabbit('John');
      alert( rabbit.kane );// i should get aaaa but i am getting undefined
     </script>
     </body>
     </html>

i should get aaaa in the alert box but how can i do a prototypical inheritance in this situation WHEN TO USE animal.prototype and when to use new animal()

1 Answers1

-2
  kane:"aaaaa";

incorrect syntax here , it should be this.kane = "aaaaa";

now for Rabbit instances to get the property kane,you can use constructor stealing like

 function Rabbit(name) {
     animal.call(this); // constructor stealing
      this.name = name;
 } 

Another approach would be

var a=new animal("aaaa");
var rabbit  = Object.create(a);

in this approach constructor stealing is not required since your directly inheriting from an instance of animal

Ramanlfc
  • 8,283
  • 1
  • 18
  • 24
  • i did it but of no use still can't alert aaaaa – Crotic Paradox Nov 26 '15 at 09:27
  • Thanks ! some issues with my Adobe Brackets – Crotic Paradox Nov 26 '15 at 09:33
  • 1
    If you set the prototype of Child to an instance of Parent then it usually shows a lack of understanding what prototype is. The correct answer should be: `Rabbit.prototype = Object.create(animal.prototype)` assuming animal is a constructor function and not an object literal (incorrect code suggest it could go both ways). If animal is an object literal then: 'Rabbit.prototype = Object.create(animal)` – HMR Nov 26 '15 at 09:53
  • @DvmSrikantXlador See my comment for the correct solution, for info on JavaScript prototype you can read the following: http://stackoverflow.com/a/16063711/1641941 and if you realy want to learn a lot and have the time for it then currently I think the following is currently the best "book" https://github.com/getify/You-Dont-Know-JS – HMR Nov 26 '15 at 09:55
  • @HMR when should i use Rabbit.prototype.__proto__=animal.prototype. if yes should animal be a class ? – Crotic Paradox Nov 26 '15 at 09:59
  • @Ramanlfc: As HMR said, you [should not use `= new animal()`](https://stackoverflow.com/questions/12592913/what-is-the-reason-to-use-the-new-keyword-here) for creating prototypes. In fact the OP's pattern was fine, even if using `Object.create` is better practise. – Bergi Nov 26 '15 at 10:55
  • @Bergi you are in serious need of reading the first answer http://stackoverflow.com/questions/383402/is-javascripts-new-keyword-considered-harmful – Ramanlfc Nov 26 '15 at 11:01
  • @Ramanlfc: I have, now what? It doesn't seem to be related to the point. Notice that I'm not advocating against `new` in general, but against using it *for creating prototype objects*. – Bergi Nov 26 '15 at 11:05
  • @Ramanlfc The first answer mentions Crockford; not the best source to learn about constructor function patterns. He has yet to produce a correct example, claims parent constructor can't be re used, wants private members but breaks encapsulation by modifying Function.prototype in his "sugar" examples. I agree with Bergi that "new" is not bad but it creates an INSTANCE of a type so using that as a shared object for Child INSTANCES (plural) shows a lack of understanding. It may work by accident but does not show you want it to work by intension (unless commented for the reason to do so). – HMR Nov 27 '15 at 02:04
  • @DvmSrikantXlador You could use it in theory but is an exotic way of doing inheritance. It is a slow operation (although setting it only once for inheritance should not be noticable) and is described as part of ES6 as a "legacy feature to ensure compatibility" (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto) Most common implementation is to use `Object.create(parent.prototype)` and repair the constructor 'Child.prototype.constructor=Child' – HMR Nov 27 '15 at 02:17
  • @HMR object.create(animal.prototype) doesn't work in this case seems like Ramanlfc answer makes my misconception wipe out and i got my required output and can u please tell why did you specify object.create() in this case – Crotic Paradox Nov 30 '15 at 18:59
  • @DvmSrikantXlador Could you please read some of the reference links so you at least know what prototype and Object.create is? By now you should know that setting Child prototype (shared among instances) to an instance of Parent (possibly having instance specific mutable members) is wrong and error prone. There is only so many times I can repeat myself. The answer has 2 things wrong: 1: using `__proto__` to set prototype is not commenly used and described as "legacy feature to ensure compatibility" 2: setting prototype of Child to an instance of Parent shows lack of understanding of prototype. – HMR Dec 01 '15 at 06:02
  • @DvmSrikantXlador Could you please update your question so we have a little more than "it does not work" to go on? – HMR Dec 01 '15 at 06:02