JavaScript JS prototype inheritance for custom classes

There is two constructor functions: Animal and Rabbit (classes emulation). How can I set inheritance for this classes using prototypes properly? I seen 2 versions:

Rabbit.prototype.__proto__ = Animal.prototype;

I think it works good as I expected. But this version doesn't work in IE10-, and there is the following version:

Rabbit.prototype = Object.create(Animal.prototype);

This version looks good too, but I must create an additional object for it. Which version is preferred? May be there is anything else? Thanks in advance.

Answer:1

The version Rabbit.prototype = Object.create(Animal.prototype); is prefered.

Mozilla Developer Network (MDN) gives the following warning about __proto__:

Warning: Changing the [[Prototype]] of an object is, by the nature of how modern JavaScript engines optimize property accesses, a very slow operation, in every browser and JavaScript engine. The effects on performance of altering inheritance are subtle and far-flung, and are not limited to simply the time spent in obj.proto = ... statement, but may extend to any code that has access to any object whose [[Prototype]] has been altered. If you care about performance you should avoid setting the [[Prototype]] of an object. Instead, create a new object with the desired [[Prototype]] using Object.create().

Answer:2

You should have something like this:

Rabbit.prototype = Object.create(Animal.prototype);
Rabbit.prototype.constructor = Rabbit;

If you won't write second line instance of Rabbit will have Animal constructor.

Also it's possible to do:

Rabbit.prototype = new Animal();

but in this case you will also trigger constructor, which is not preferred.

Answer:3

I m dynamically generating 20 input field type of array in html and submitting record and storing in database. For Example for particular one input field is coded as below. similar there are other 20 ...

I m dynamically generating 20 input field type of array in html and submitting record and storing in database. For Example for particular one input field is coded as below. similar there are other 20 ...

When I use the below simple SVG it works well with the rotation method var car = "M17.402,0H5.643C2.526,0,0,3.467,0,6.584v34.804c0,3.116,2.526,5.644,5.643,5.644h11.759c3.116,0,5.644-2.527,5.644-5....

When I use the below simple SVG it works well with the rotation method var car = "M17.402,0H5.643C2.526,0,0,3.467,0,6.584v34.804c0,3.116,2.526,5.644,5.643,5.644h11.759c3.116,0,5.644-2.527,5.644-5....

  1. rotate marker in google maps
  2. rotate marker in leaflet
  3. rotate marker on map
  4. rotate marker in gmap.net
  5. rotate marker on map android
  6. rotate marker symbols with dataframe
  7. rotate marker google maps
  8. rotate marker icon google maps
  9. google map marker rotation

I'm using React Navigation, I want to add right button in StackNavigator. After adding right button, I get such error: Object is not valid as React child (found: object with keys {right}). If ...

I'm using React Navigation, I want to add right button in StackNavigator. After adding right button, I get such error: Object is not valid as React child (found: object with keys {right}). If ...

  1. objects are not valid react child

In this example the ::before pseudo-element's content is not a static value, but a counter. I want to get the current value "2" instead of the general formula "counter(itemcounter)". // Expected ...

In this example the ::before pseudo-element's content is not a static value, but a counter. I want to get the current value "2" instead of the general formula "counter(itemcounter)". // Expected ...

  1. css text content before
  2. css add text before content
  3. content before text