Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any benefit to using prototype instead of declaring properties on the object itself?

A prototype is used to declare properties and methods for a class of objects. One advantage of using prototype is that it conserves memory because all instances of a class point to the properties and methods of the prototype which conserves memory and effectively allows properties to be treated as static by all instances of a class.

Prototype is used for inheritance through prototype chaining.

My question is very simple. Why use prototype at all when you can just do:

function car() {
    this.engine = "v8";
}
function mustang() {
    // nm, no good way to inherit without using prototypes
}

Is that right? So the primary purpose of prototypes is threefold:

  1. conserve memory
  2. provide static properties
  3. is the only way for a reference type to inherit from a super class
like image 390
user1472219 Avatar asked Jul 02 '12 13:07

user1472219


People also ask

What is the advantage of prototype in JavaScript?

Prototypes allow you to easily define methods to all instances of a particular object. The beauty is that the method is applied to the prototype, so it is only stored in the memory once, but every instance of the object has access to it.

When would you use a prototype?

Prototype model should be used when the desired system needs to have a lot of interaction with the end users. Typically, online systems, web interfaces have a very high amount of interaction with end users, are best suited for Prototype model.

Can we create an object without prototype?

You could potentially "create" such an object by instantiating a new environment ("realm" in ES6 terms), e.g. via an <iframe> , capturing its Object. prototype , stripping it of its properties and voilá - you've got a new empty object.

Do objects have prototype property?

Every object in JavaScript has a built-in property, which is called its prototype. The prototype is itself an object, so the prototype will have its own prototype, making what's called a prototype chain. The chain ends when we reach a prototype that has null for its own prototype.


1 Answers

conserve memory

Yes it does, when you create hundreds of instances of Car and they all have their own functions (that have their own closure scopes) you'll consume more memory.

Can't find a reference for it but it has been suggested that Chrome optimises constructor functions that use prototype better than constructor functions with everything in the constructor body.

provide static properties

Static is more like Date.now(), every instance has members from the prototype but can be called on the instance.

is the only way for a reference type to inherit from a super class

You can inherit with Parent.apply(this,arguments); in Child but it makes extending Parent functions more complicated and doesn't make childInstance instanceof Parent true. What that code does is run Parent code with the to be created Child instance as the invoking object (this). Inheritance is usually done in 2 places.

  1. In the Child body Parent.apply(this,arguments); to re use Parent initialisation code and make Parent instance members to be Child instance members (for example: this.name).
  2. Setting Child.prototype to shallow copy of Parent.prototype Child.prototype=Object.create(Parent.prototype);Child.prototype.constructor=Child; This will ensure that shared Parent members are available on Child instances (like the function getName).

These points are explained in more detail here: https://stackoverflow.com/a/16063711/1641941

like image 163
HMR Avatar answered Sep 21 '22 11:09

HMR