Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

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

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.

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.

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.

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';
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top