There are a few ways to get class-like behavior in javascript, the most common seem to be prototype based like this:

function Vector(x, y, x) {
    this.x = x;
    this.y = y;
    this.z = z;
    return this;
}

Vector.prototype.length = function () { return Math.sqrt(this.x * this.x ... ); }

and closure based approaches similar to

function Vector(x, y, z) {
    this.length = function() { return Math.sqrt(x * x + ...); }
}

For various reasons the latter is faster, but I've seen (and I frequently do write) the prototype version and was curious as to what other people do.

7 Answers

10
Kieron On Best Solutions

Assigning functions to the prototype is better (for public methods) because all instances of the class will share the same copy of the method. If you assign the function inside the constructor as in the second example, every time you create a new instance, the constructor creates a new copy of the length function and assigns it to just that one instance.

However this latter technique is useful if you want each copy to have it's own copy, the main use of that being to do private/privileges methods which have access to private variables declared inside the constructor and inherited via the closure mechanism.

Douglas Crockford has a good summary.

3
Adhip Gupta On

Well, I don't really have an expert opinion on this. I usually end up using closures based approach just because it keeps the code simpler to manager. But, I have found myself using prototypes for methods that have loads of lines of code.

2
Patrick On

You also have the choice of:

function Vector(x, y, z) {
  function length() {
    return Math.sqrt(x * x + ...);
  }
}

Which is probably just as slow as example two, but it looks more like Java/C# and is a bit more explicit.

3
Orion Edwards On

Fortunately I get to use prototype.js, which provides some nice wrappers. So you can do this:

var Person = Class.create({
    initialize: function(name) {
        this.name = name;
    },
    say: function(message) {
        return this.name + ': ' + message;
    }
});

Prototype.js Documentation: Defining classes and inheritance

4
JayTee On

There is also the object literal approach to the prototype:

var Vector = function(){};

Vector.prototype = {
  init:function(x,y,z) {
    this.x = x;
    this.y = y;
    this.z = z;
  },
  length:function() {
    return Math.sqrt(x * x + ...);
  }
};

var v1 = new Vector();
v1.init(1,2,3);
1
rfunduk On

I'm a big fan of using John Resig's library for this. Lightweight, straight-forward, and you already know how to use it if you're familiar with the 'usual' object-oriented style.

1
James Drinkard On

There are no classes in javascript.

There are objects however. You don't need a class to create an object in javascript. It does have constuctor functions that you can invoke with new for example:

var james = new Person();

You can simulate class like behavior with:

prototype example:

function Car (type) {
    this.type = type;
    this.color = "red";
}

Car.prototype.getInfo = function() {
    return this.color + ' ' + this.type + ' car';
};

object literal example

var car = {
    type: "honda",
    color: "red",
    getInfo: function () {
        return this.color + ' ' + this.type + ' car';
    }
}