Search code examples
javascriptobjectprototype-programming

What is the difference between Something.prototype.else and Something.else


Possible Duplicate:
JavaScript: Class.method vs. Class.prototype.method

I am trying to understand object in JavaScript. Now I see a lot of different uses of object, and I can not tell them apart.

For starters, the biggest thing I want to know is what the difference is between these two

Something.prototype.else = function(){
  return 6;
}

And

Something.else = function(){
  return 6;
}

Both look different, but they are used in the same way, or am I mistaken.


Solution

  • If you are familiar with other programming languages you can consider the second one to be a static method.

    The first one you need an instance of the object in order to use it:

    var x = new Something();
    x.else();
    

    The second one you do not need an instance in order to use it:

    Something.else();