/ Published in: JavaScript
Assign methods to a Function prototype when you intend to create instances of the type of object. Use object literals when you just need a container for functions.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// consider: function Grape(type) { this.type = type || 'Xynomavro'; } Grape.prototype.squash = function () { return this.type.substring(0, 3); }; var grape = new Grape(); alert(grape.squash()); // versus: var grapes = { barrel : [], squash : function (grape) { return grape.substring(0, 3); } }; grapes.barrel.push('Nebbiolo'); alert(grapes.squash(grapes.barrel[0]));