5

I was wondering, what is the diferrence between a prototyped and a non-prototyped method in JavaScript? Any help is deeply appreciated.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Alfa9Dev
  • 81
  • 4
  • MDN Explanation of prototype here -> https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/prototype – Manse May 22 '12 at 15:19
  • One is defined on the object itself, the other on the object's prototype. By definition. – Matt Ball May 22 '12 at 15:19

1 Answers1

10

A non-prototyped method will take up memory in every instance of the class.

It will also (assuming it's declared in the scope of the class constructor) have access to any other private variables (or methods) declared in that scope.

For example, this will create an instance of the function per object, and that function can access myVar:

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

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

function MyObject() {
     var myVar;
};

MyObject.prototype.func = function() { ... };
Alnitak
  • 334,560
  • 70
  • 407
  • 495