网络编程
位置:首页>> 网络编程>> JavaScript>> javascript设计模式交流(二) Prototype Pattern(2)

javascript设计模式交流(二) Prototype Pattern(2)

作者:winter 来源:无忧脚本 发布时间:2007-11-29 14:01:00 

标签:Prototype,Pattern,模式,javascript

内置对象的clone
    但是,到这里为止,我们还没有考虑内置对象,内置对象不能用普通方法clone 我们要考虑的内置对象有这么几个:
        Function Array String Boolean Number Date
    RegExp Error和Math没有需要clone的场景 所以不在我们的考虑之中。
    对Function来说,完全产生一个副本是不可能的,因为我们无法保证构造的函数跟原来的函数在同一作用域,但是不包含作用域的实现是很容易的:
        eval(this);
或者使用Function构造
        return Function(new String("return ")+this)();
Function本身是个Object 因此必须加上Object的clone 实现functionPrototypeClone需要一点小花招


/************************************/
        function functionClone()
        {
                var ret=Function(new String("return ")+this)();
                for(var p in this)
                {
                        ret[p]=this[p];
                }
                return ret;
        }
/************************************/
        function functionDeepClone()
        {
                var ret=Function(new String("return ")+this)()
                for(var p in this)
                {
                        if(typeof ret[p]!=object)ret[p]=this[p];
                        ret[p]=objectDeepClone.call(this[p]);
                }
                return ret;
        }
/************************************/
        function functionPrototypeClone()
        {                
                var tmp=Function.prototype;
                Function.prototype=this;
                var ret=(new Function(new String("return ")+this))();
                Function.prototype=tmp;
                return ret;
        }
/************************************/ 


Array只要保证length正确就可以了


/************************************/
        function arrayClone()
        {
                var ret=new Array();
                for(var p in this)
                {
                        ret[p]=this[p];
                }
                return ret;
        }
/************************************/
        function arrayDeepClone()
        {
                var ret=new Array();
                for(var p in this)
                {
                        if(typeof ret[p]!=object)ret[p]=this[p];
                        ret[p]=objectDeepClone.call(this[p]);
                }
                return ret;
        }
/************************************/
        function arrayPrototypeClone()
        {               
                var tmp=Array.prototype;
                Array.prototype=this;
                var ret=new Array();
                Array.prototype=tmp;
                return ret;
        }
/************************************/

Date对象提供了getTime 所以可以很容易实现


/************************************/
        function dateClone()
        {
                var ret=new Date();
                ret.setTime(this.getTime());
                for(var p in this)
                {
                        ret[p]=this[p];
                }
                return ret;
        }
/************************************/
        function dateDeepClone()
        {
                var ret=new Date();
                ret.setTime(this.getTime());

                for(var p in this)
                {
                        if(typeof ret[p]!=object)ret[p]=this[p];
                        ret[p]=objectDeepClone.call(this[p]);
                }
                return ret;
        }
/************************************/
        function datePrototypeClone()
        {               
                var tmp=Date.prototype;
                Date.prototype=this;
                var ret=new Date();
                ret.setTime(this.getTime());
                Date.prototype=tmp;
                return ret;
        }
/************************************/

 

String Boolean Number都是只读的对象,所以只要=就可以了。

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com