js valueof与toSting,对象的复制

        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;
        }


valueof 与toString() 区别

 

                var obj = {

                    i: 10,

                    valueOf: function () { return this.i + 30; },

                    toString: function () { return this.valueOf() + 10; }

                }

                alert(obj > 20); // true

                alert(+obj); // 40

                alert(obj); // 50


posted @ 2012-12-27 02:01  至道中和  阅读(354)  评论(0编辑  收藏  举报