JScript中没有clone方法,自己写个玩clone玩玩

Object.prototype.Clone = function()
 {
    var objClone;
    if ( this.constructor == Object ) objClone = new this.constructor();
    else objClone = new this.constructor(this.valueOf());
    for ( var key in this )
    {
        if ( objClone[key] != this[key] )
        {
            if ( typeof(this[key]) == 'object' )
            {
                objClone[key] = this[key].Clone();
            }
            else
            {
                objClone[key] = this[key];
            }
        }
    }
    objClone.toString = this.toString;
    objClone.valueOf = this.valueOf;
    return objClone;
 }   

你可能感兴趣的:(prototype)