Prototypal inheritance

by Douglas Enas

JavaScript

var BaseObject = function() {
    _getBinding = function(method) {
        var _self = this;
        return function() {
            _self[method].apply(_self, arguments);
        };
    };
    return {
        CallInline: _getBinding
    }
}();


var TestObj = function(value) {
    $.extend(this, BaseObject);
    // public var
    this._value = value;
};

TestObj.prototype = function() {
    var privateVar = false;
    // these are private
    _giveMe = function() {
        return this._value;
    }, _callMeLaterTestObj = function() {
        console.log('I am ' + this.constructor.name + ' my value is ' + this._value);
    };

    // public API
    return {
        GiveMe: _giveMe,
        CallMeLaterTestObj: _callMeLaterTestObj
    }

}();

function CallMeLater(v, i) {
    setTimeout(v.CallInline('CallMeLaterTestObj'), 10);
}



var V1 = new TestObj(1);
var V2 = new TestObj(2);
var V3 = new TestObj(3);


console.log('V1= ' + V1.GiveMe());
console.log('V2= ' + V2.GiveMe());
console.log('V3= ' + V3.GiveMe());
console.log('---');

V1.CallMeLaterTestObj();

console.log('---');

CallMeLater(V1, 1);
CallMeLater(V2, 2);
CallMeLater(V3, 3);