Simple function to instantiate Circles. Note the ability to accept random parameter properties, overwrite default properties, and limit the set of permissible parameter properties.
var Circle = function(properties) { var newCircle = { radius: 2, getRadius: function() { return this.radius; } }; for (o in properties) { if (! o.match(/b(radius|hollow)b/i)) { console.log("deleting "+o); var x = delete o; console.log(x); } } jQuery.extend(newCircle, properties); return newCircle; } var x1 = new Circle({radius:3}); var x2 = new Circle({radius:5, trojan: 10, hollow: "true"}); var x3 = new Circle(); console.log(x1.getRadius()); console.log(x2.getRadius()); console.log(x3.getRadius());
The code above uses a jQuery function to extend our object with the properties object. This approach would almost always be acceptable to me, but what if a client felt it was necessary to create private variables and force their users to stick with the API? We can use closures to get private instance variables.
var Circle = function(properties) { var radius = 2; var newCircle = { getRadius: function() { return radius; }, setRadius: function(num) { radius = num; return true; } }; for (o in properties) { if (! o.match(/b(radius|hollow)b/i)) { console.log("deleting "+o); var x = delete o; console.log(x); } else if (o.match(/b(radius)b/)) { newCircle.setRadius(properties[o]); } } return newCircle; } var x1 = new Circle({radius:3}); var x2 = new Circle({radius:5, trojan: 10, hollow: "true"}); var x3 = new Circle(); console.log(x1.getRadius()); console.log(x2.getRadius()); console.log(x3.getRadius()); console.log(x1.radius);

