Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript OOP: method definition with or without "prototype"

Is this code,

function Person() {
    function  myMethod() {
        alert ('hello');
    }
    this.method = myMethod;
}

equivalent to:

function Person() {    }
Person.prototype.method2  = function() {
    alert ('hello');
};

If yes, which method definition should I use and why?

like image 764
Jean-Philippe Martin Avatar asked May 08 '12 14:05

Jean-Philippe Martin


People also ask

Can we create an object without prototype in JavaScript?

var empty = {}; // Outputs: Function Object() console. log(empty. constructor); Every time you create a new object via an object literal (the {} ), behind the scenes JavaScript invokes the Object constructor to create the object, just as if you'd used new Object() .

Which object in JavaScript does not have a prototype?

JavaScript has two types of objects: function object and non-function object. Conceptually, all objects have a prototype (NOT A PROTOTYPE PROPERTY).

Should you use prototype in JavaScript?

There is a clear reason why you should use prototypes when creating classes in JavaScript. They use less memory. When a method is defined using this. methodName a new copy is created every time a new object is instantiated.

Should I use prototype or class JavaScript?

To answer your question simply, there is no real difference. Straight from the MDN web docs definition: JavaScript classes, introduced in ECMAScript 2015, are primarily syntactical sugar over JavaScript's existing prototype-based inheritance.


2 Answers

They are functionally equivalent in your simple example, but behind the scenes work very differently. The prototype property on a function is really the "prototype template". It says "whenever an object is made and I am used as the object's constructor, give them this object as their prototype".

So all Persons created in your second example share the same copy of the method2 method.

In the first example, each time the interpreter encounters the function keyword, then it creates a new function object. So in the first example, each instance of Person has their own copy of the myMethod method. The vast majority of the time this doesn't matter. But this first approach uses more memory, and sometimes that does matter.

They are not functionally equivalent in more interesting cases. In the first example, myMethod can access local variables defined in Person, but the second example cannot, as one difference.

like image 143
Matt Greer Avatar answered Oct 27 '22 03:10

Matt Greer


In the first scenario, when you create a new person, var person1 = new Person();, it will have its own copy of myMethod. If you create 100 Person objects, they will each have their own copy of this method.

Using a prototype, every new Person object will share the method definition. This is much more memory efficient since there will only be one copy of the method.

If you are planning on having several Person objects, the second way is better.. but if there are only a few Person objects, it won't matter that much.

like image 26
Brian Glaz Avatar answered Oct 27 '22 04:10

Brian Glaz