Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between extending an object using prototype or inline? [duplicate]

Tags:

javascript

What's the difference? Is there any?

var Likes = function (el) {
  this.el = $(el);
  return this;
};

Likes.prototype.add = function (name) {
  this.el.find('.no-results').remove();
  $('<li>', { text: name }).appendTo(this.el);
};

and:

var Likes = function (el) {
  this.el = $(el);
  this.add = function (name) {
    this.el.find('.no-results').remove();
    $('<li>', { text: name }).appendTo(this.el);
  };
  return this;
};
like image 940
rball Avatar asked May 24 '13 15:05

rball


People also ask

What is the difference between prototype and object in JavaScript?

A prototype is just an object. It's any object that another object uses as it's prototype.

What is the difference between prototype and __ proto __ explain with examples?

The prototype property is set to function when it is declared. All the functions have a prototype property. proto property that is set to an object when it is created using a new keyword. All objects behavior newly created have proto properties.

What is the prototype of an object?

The prototype of an object is referred to by the prototype property of the constructor function that creates and initializes the object. The isPrototypeOf() method provides a way to determine if one object is the prototype of another. This technique can be used to determine the class of an object.

How do you extend an object?

The extends keyword can be used to extend the objects as well as classes in JavaScript. It is usually used to create a class which is child of another class. Syntax: class childclass extends parentclass {...}


3 Answers

The difference is in how the object gets created. When you define functions on an object's prototype, it's defined ONCE for every further instance of that object.

If you declare functions on an instance level, they get redefined each time you declare the function.

It actually has a performance impact http://jsperf.com/prototype-vs-instance-functions

It's generally considered a best practice to use the prototype for functions that will be re-used on multiple instances of a constructor. For example if you are using the new operator to create instances of a constructor..

var Likes = function (el) {
  this.el = $(el);
  return this;
};

Likes.prototype.add = function (name) {
  this.el.find('.no-results').remove();
  $('<li>', { text: name }).appendTo(this.el);
};

var oneLike = new Likes();
var twoLike = new Likes();
var threeLike = new Likes();

Since the add is defined on the object's prototype, it gets define just one time rather than each time the Likes is instantiated.

like image 60
jcreamer898 Avatar answered Sep 18 '22 00:09

jcreamer898


Yes, there is a difference.

If you uses prototype object, then all created objects of 'Likes' will have same reference to prototype object. But if you use second method (this.add), it will add function to every created object.

First one is more prefer method than second one.

like image 31
Fizer Khan Avatar answered Sep 20 '22 00:09

Fizer Khan


Example 2 is the better practice because it lends itself to implementing inheritance rather than making wasteful copies of object properties.

In a small application with no inheritance, there is probably not much of a practical difference between the two examples. But imagine that you had 10000 instances of the Likes constructor in a more complex application using inheritance. With the second example, each one of them will receive a copy of the add function locally.

This could theoretically cause memory bottlenecks in a larger application. Also, if you want to change the add method in the future, you would need to do so on each local object.

like image 23
Dan Avatar answered Sep 19 '22 00:09

Dan