Read More Ninja Action!

JavaScript Object Constructor

A colleague of mine asked me an interesting question today..."how can I create a constructor for a custom object in JavaScript?" At first, I had no idea what the hell he was talking about, but then after sounding out each word slowly I got the picture.

I didn't actually know the answer, so I took just a few minutes to hack out my idea of how it would work and I ended up with this...

function Obj() {
  this.Property = null;
  var Constructor = function(obj) {
 obj.Property = "sexy";
  };
 Constructor(this);  
}

var test = new Obj();
console.log(test.Property);

Basically you can see that on instantiation of the new JavaScript the internal constructor method gets executed.

I'm sure some JavaScript genius is reading this post and laughing, but fuck you...I thought it was useful!