0

I've always seen that the preferred alternative to

function MyObject ( ... )
{
    this.MyFunction = function ( ... ) { ... } 
    // ... 
}

is

function MyObject ( ... )
{
    // ... 
}

MyObject.prototype.MyFunction = function ( ... ) { ... } 

My understanding is that in the way that the JavaScript hierarchy known as the prototype chain works, MyObject.prototype is the object from which MyObject derives. In English, what this is saying is

"Give me a property by giving it to my ancestor who I will then inherit it from."

which seems like a very obscure and inefficient way of going about things. Coming from a C# background, how I see this is like the equivalent of having a class

class SpecialString : String 
{

}

and saying

Ok, I want SpecialString to have a function SpecialFunction, so I'll go into String and give it public void SpecialString (...). Now SpecialString has it! Mission accomplished.

Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
Subpar Web Dev
  • 3,210
  • 7
  • 21
  • 35
  • No, it's not `MyObject` which will inherit it, but all `new MyObject` instances you are going to create – Bergi Dec 17 '15 at 22:51
  • Or put another way: `MyObject.prototype` is not the prototype of `MyObject` (not the object which it inherits from). `MyObject` is a constructor function and inherits from `Function.prototype` - the prototype object for all functions. – Bergi Dec 17 '15 at 22:53
  • Actually JavaScript inheritance model is more efficient than classical model. Every instance of MyObject will have a pointer to MyObject.prototype, that can be used to use methods that every instance can share (that saves lots of space). – sergeyz Dec 17 '15 at 22:54

2 Answers2

2

Ok, I want SpecialString to have a function SpecialFunction, so I'll go into String and give it public void SpecialString (...). Now SpecialString has it! Mission accomplished.

Not at all! Using your example, that would be like doing the following:

function MyObject ( ... )
{
    // ... 
}
Object.prototype.MyFunction = function ( ... ) { ... }

Adding the method to the base Object, so that by-extension your custom object has it also.


Each constructor get's their own prototype object, where class-like methods can be defined.

The prototype is a flexible way of defining a property or method which can be shared across all instances of an object, without the need to re-declare and create a new function object every time a new instance is created.

Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
0

With prototype, the function is shared between all instances. Without prototype, the function is duplicated : There is an instance of the function for each instance of the class (it's not a true class but you see what I mean). You can say the function declaration on the prototype consume less memory than the other way. There is some advantages for both but the prototype way is more standard and allow to create a behavior more near from C# class IMHO.

t.ouvre
  • 2,856
  • 1
  • 11
  • 17