文章出處

函數式繼承:

var object = function (obj) {
    if (typeof Object.create !== 'undefined') {
        return Object.create(obj);
    } else {
        var F = function () {};
        F.prototype = obj;
        return new F();        
    }

};

淺復制繼承:

function extend(Parent, Child) {
    var Child = Child || {},
        i;
    for (i in Parent) {
        Child[i] = Parent[i];
    }
    return Child;
}

深復制:

function deepCopy(Parent, Child) {
    var Child = Child || {},
        toStr = Object.prototype.toString,
        astr = "[object Array]",
        i;

    for (i in Parent) {
        if (typeof Parent[i] === 'object') {
            Child[i] = toStr.apply(Parent[i]) === astr ? [] : {};
            deepCopy(Parent[i], Child[i]);
        } else {
            Child[i] = Parent[i];
        }
    }

    return Child;
}

函數綁定和借用:

function method(o, m) {
    return function () {
        return m.apply(o, [].slice.call(arguments));
    };
}


if (typeof Function.prototype.bind === "undefined") {
    Function.prototype.bind = function (thisArg) {
        var fn = this,
            slice = Array.prototype.slice,
            args = slice.call(arguments, 1);
        return function () {
            return fn.apply(thisArg, args.concat(slice.call(arguments)));    
        };    
    };
}

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()