-1

What is the difference between the following two class implementations

function Animal() {
  this.feed = function () {
  }
}


function Animal () {
}

Animal.prototype.feed = function () {
};

I tried creating both the function but both worked in a similar way.

fool-dev
  • 7,671
  • 9
  • 40
  • 54
Bhavesh Shah
  • 51
  • 1
  • 7
  • 1
    No difference, The first class is you have the method within class, and other is you have extended the class via prototype. It all makes sense in how to maintain and structure the class in your application – Premalatha Feb 09 '18 at 05:27
  • Refer link below:- https://stackoverflow.com/questions/12180790/defining-methods-via-prototype-vs-using-this-in-the-constructor-really-a-perfo – Pratheesh P S Feb 09 '18 at 05:39
  • Bhavesh I have posted an answer but still if you have confusion let me know I shall try to explain in more proper way. – Dipak Feb 09 '18 at 06:10

2 Answers2

1

Although they are functionally similar there will definitely be a difference in performance. In the first case, you assign a new function instance to the object every time the class is instantiated, this can take a toll in both CPU and Memory usage. The second version assigns the function to the prototype, so the function object is only created once and only assigned once.

Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357
0

Generally, people think that Javascript is not an object-oriented programming language.

Constructor function

function Animal(){
    this.feed = function(){
    }
}

constructor function automatically sets a prototype object for newly created objects. This prototype object is stored in the ConstructorFunction.prototypeproperty. In constructor function, it stores all the properties and methods in the new object.

The constructor function is JavaScript's version of a class. You'll notice that it has all the features you'd expect in a function but still, it returns nothing. this keyword is basically saying that whenever one of these object instances is created, the object's property will be equal to the name value passed to the constructor call, and the feed() method will use to the constructor call too.

Prototype inheritance

function Animal () {
}

Animal.prototype.feed = function () {
};

The prototype is mechanism which uses to inherits other features, but it works differently than classical inheritance. Prototype acts as a template which inherits properties and how it acts. Object prototype also has a prototype object which inherits method and properties from core prototype object.

This is called prototype chain. proto the, which is derived from the prototype property on the constructor. In javascript prototype inheritance, it actually does not copy the object, but link is made between the object instance and its prototype

In Prototype inheritance, it creates link or reference of a class to object. Every object can have another object as its prototype. Then the former object inherits all of its prototype’s properties. An object specifies its prototype via the internal property [[Prototype]].

Please review below reference links which have really a very good description.

https://hackernoon.com/understand-nodejs-javascript-object-inheritance-proto-prototype-class-9bd951700b29

https://www.digitalocean.com/community/tutorials/understanding-prototypes-and-inheritance-in-javascript

https://javascript.info/prototype-inheritance

Dipak
  • 2,248
  • 4
  • 22
  • 55