tags:

views:

293

answers:

6

There are a few ways to get class-like behaviour 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.

+1  A: 

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.

Adhip Gupta
+1  A: 

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.

Patrick
+6  A: 

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.

Kieron
+2  A: 

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;
}
});

Protype.js Documentation

Orion Edwards
+3  A: 

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);
JayTee
A: 

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.

thenduks