2

I am reading about prototypes in javascript, In a article http://phrogz.net/js/classes/OOPinJS.html I read that we can not assign public methods inside a object constructor in javascript ? How prototypal methods are different from static methods and what are the advantage of using them ?

Anshul
  • 9,312
  • 11
  • 57
  • 74
  • possible duplicate of [Declaring javascript object method in constructor function vs. in prototype](http://stackoverflow.com/questions/9772307/declaring-javascript-object-method-in-constructor-function-vs-in-prototype) – bfavaretto Feb 18 '13 at 04:22
  • 1
    The public methods need to be declared in the prototype (which is done outside of the constructor). – Ja͢ck Feb 18 '13 at 04:41

3 Answers3

2

JavaScript isn't really well suited for most OOP concepts and paradigms, especially once you try to emulate inheritance. Rather than think of prototype vs "privileged" methods in OOP terms, you should think of things in terms of how JavaScript instantiates objects. Take this simple "class":

var id = 0;

function myClass()
{
     var that = this;
     id++; //closure, each new instance gets a unique id
     this.id = id;

     this.toString = function()
     {
         return that.id.toString();
     }
}

And this class is instantiated like so:

var classInstance = new myClass();

This isn't a pattern I would necessarily recommend, the point is to illustrate that for each instantiation, each instance gets its own unique toString function. That means if you instantiate 100 classInstances, and you change toString on one of them to do something else, only that one instance will have that new functionality.

That also means that for every instance, every privileged method is also instantiated alongside with it. If you are instantiating a lot of instances, that can make a big performance difference. I had a case where I saw a measurable speed improvement by converting my privileged methods to prototype methods.

Speaking of prototype methods, here's what that might look like:

var id = 0;

function myClass()
{
     id++; //closure, each new instance gets a unique id
     this.id = id;
}

myClass.prototype.toString = function()
{
    return this.id.toString();
}

In this case no matter how many myClasses you have, you only instantiate the toString method once. And if it changes, it changes for all myClasses.

Personally, I use privileged methods in most of my JavaScript classes because it looks cleaner and only bother with the prototype chain if I know it's going to be instantiated a huge number of times. Also being able to access private variables allows you to have some semblance of information hiding vs being forced to make any accessed variables public.

lmortenson
  • 1,610
  • 11
  • 11
2

I read that we can not assign public methods inside a object constructor in javascript?

Yes, the article refers to this:

function MyObj(name)
{
    this.name = name;
}

MyObj.prototype.sayHello = function() {
    alert('hello ' + this.name);
}

new MyObj('world').sayHello();

As you can see, the public method sayHello() is declared in the prototype, which is done outside of the constructor. This is just how JavaScript works.

How prototypal methods are different from static methods and what are the advantage of using them ?

Prototypal methods are only "attached" to objects. For static methods you need to use this construct:

var MyStaticThing = {
    name: 'world',
    sayHello: function() {
        alert('hello ' + this.name);
    }
}

MyStaticThing.sayHello();
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
1

"the advantage of using them"...which one are you referring? Generally the advantage is going to be using a prototypal method (and object construction generally), over something like static or classical methods in javascript. The answer you are looking for is really too long to address here, but the short answer is that javascript is based off a prototypal object inheritance system (meaning objects can be created on the fly and may inherited one to the other), as opposed to a classical system (objects may only inherit from classes, i.e. Java, C++, etc.)

While you may create objects in javascript in a classical way -- because the language is that flexible -- it is a bad and confused way to do it. Prototypical object construction allows you do important and good things like data hiding, access to super methods, etc. Like I said this is a verbose subject, really to big to be taken on in a little text box.

cliffbarnes
  • 1,396
  • 11
  • 21