Prototyped and not prototyped method?

I was wondering what is the difference between prototype and non prototyped method in JavaScript? Any help is greatly appreciated.

+5
source share
1 answer

A nonprotected method will occupy memory in each instance of the class.

It also (provided that it is declared as part of the class constructor class) has access to any other private variables (or methods) declared in this area.

For example, this will create an instance of a function for each object, and this function will be able to access myVar:

function MyObject() {
     var myVar;
     this.func = function() { ... };
};

and in this case there is only one instance of the function shared between each instance of the object, but it will not have access to myVar:

function MyObject() {
     var myVar;
};

MyObject.prototype.func = function() { ... };
+10

All Articles