Friday, July 27, 2012

Extension Method in Javascript

    //Define Extension Method over here
    if (!Array.prototype.forEach_Murli) {
        Array.prototype.forEach_Murli = function(fun) {
            var len = this.length;
            if (typeof fun != "function")
                throw new TypeError();

            var thisp = arguments[1];
            for (var i = 0; i < len; i++) {
                if (i in this)
                    fun.call(thisp, this[i], i, this);
            }
        };
    }

    function printBr(element, index, array) {
        document.write("
[" + index + "] is " + element); } //Call the Extension Method [12, 5, 8, 130, 44].forEach_Murli(printBr);

No comments:

Post a Comment