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

<pre><code>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 ... ); }
</code></pre>

<p>and closure based approaches similar to</p>

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

<p>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.</p>

[{'answer_id': 6508, 'author': 'Adhip Gupta', 'author_id': 384, 'author_profile': 'https://Stackoverflow.com/users/384', 'pm_score': 2, 'selected': False, 'text': "<p>Well, I don't really have an expert opinion on this.\nI 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.</p>\n"}, {'answer_id': 6516, 'author': 'Patrick', 'author_id': 429, 'author_profile': 'https://Stackoverflow.com/users/429', 'pm_score': 2, 'selected': False, 'text': '<p>You also have the choice of:</p>\n\n<pre><code>function Vector(x, y, z) {\n  function length() {\n    return Math.sqrt(x * x + ...);\n  }\n}\n</code></pre>\n\n<p>Which is probably just as slow as example two, but it looks more like Java/C# and is a bit more explicit.</p>\n'}, {'answer_id': 6531, 'author': 'Kieron', 'author_id': 588, 'author_profile': 'https://Stackoverflow.com/users/588', 'pm_score': 5, 'selected': True, 'text': '<p>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.</p>\n\n<p>However this latter technique is useful if you <em>want</em> 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.</p>\n\n<p>Douglas Crockford has a good <a href="http://javascript.crockford.com/private.html" rel="noreferrer">summary</a>.</p>\n'}, {'answer_id': 6533, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 2, 'selected': False, 'text': '<p>Fortunately I get to use <a href="http://prototypejs.org/" rel="nofollow noreferrer">prototype.js</a>, which provides some nice wrappers. So you can do this:</p>\n\n<pre><code>var Person = Class.create({\n    initialize: function(name) {\n        this.name = name;\n    },\n    say: function(message) {\n        return this.name + \': \' + message;\n    }\n});\n</code></pre>\n\n<p><a href="http://prototypejs.org/learn/class-inheritance" rel="nofollow noreferrer"><code>Prototype.js</code> Documentation: Defining classes and inheritance</a></p>\n'}, {'answer_id': 112916, 'author': 'JayTee', 'author_id': 20153, 'author_profile': 'https://Stackoverflow.com/users/20153', 'pm_score': 3, 'selected': False, 'text': '<p>There is also the object literal approach to the prototype:</p>\n\n<pre><code>var Vector = function(){};\n\nVector.prototype = {\n  init:function(x,y,z) {\n    this.x = x;\n    this.y = y;\n    this.z = z;\n  },\n  length:function() {\n    return Math.sqrt(x * x + ...);\n  }\n};\n\nvar v1 = new Vector();\nv1.init(1,2,3);\n</code></pre>\n'}, {'answer_id': 3338461, 'author': 'rfunduk', 'author_id': 210, 'author_profile': 'https://Stackoverflow.com/users/210', 'pm_score': 1, 'selected': False, 'text': '<p>I\'m a big fan of using <a href="http://ejohn.org/blog/simple-javascript-inheritance/" rel="nofollow noreferrer">John Resig\'s library</a> for this. Lightweight, straight-forward, and you already know how to use it if you\'re familiar with the \'usual\' object-oriented style.</p>\n'}, {'answer_id': 19911320, 'author': 'James Drinkard', 'author_id': 543572, 'author_profile': 'https://Stackoverflow.com/users/543572', 'pm_score': 1, 'selected': False, 'text': '<p>There are no classes in javascript.  </p>\n\n<p>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:</p>\n\n<pre><code>var james = new Person();\n</code></pre>\n\n<p>You can simulate class like behavior with:</p>\n\n<p>prototype example:</p>\n\n<pre><code>function Car (type) {\n    this.type = type;\n    this.color = "red";\n}\n\nCar.prototype.getInfo = function() {\n    return this.color + \' \' + this.type + \' car\';\n};\n</code></pre>\n\n<p>object literal example</p>\n\n<pre><code>var car = {\n    type: "honda",\n    color: "red",\n    getInfo: function () {\n        return this.color + \' \' + this.type + \' car\';\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/6499', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/784/']