Joined November 2012
·

Errol Burger

Web Developer at Contractor
·
Cape Town, South Africa
·
·
·

Posted to JavaScript Constructors over 1 year ago

That is because at the time constructor is invoked with new, stranger is 2. Only after order.value is assigned does stranger become 3.

The self executing function may be overkill but maybe it's because I'm new to all of this that I'm finding the amusement in it:

var Order = (function(altConstructor) {
    var y = 10;
    return altConstructor || function Order(x){
      this.id = x;
      this.y = y
    }
})(
    (function(condition){ 
        /* return completely different constructor or null */ 
    })(condition)
);
Posted to JavaScript Constructors over 1 year ago

Nice. I've grown fond of doing this:

var Order = (function() {
    // initialise some stuff before the return
    return function Order(x){
        this.id = x;
    }
})();

var order = new Order(1);
Achievements
49 Karma
0 Total ProTip Views