object 深度克隆与继承关系
Object.prototype.extends = function(obj){ var object = new this.constructor; var o = this; var fo , fp , type; for( var i in o ){ if( o.hasOwnProperty(i) ) { fo = o[i]; fp = object[i]; type = Object.prototype.isType.call(fo); if( type === "array" || type === "object" ) //如果是对象 object[i] = fo.extends(fp); else object[i] = fo; } } for( var i in obj ){ if( obj.hasOwnProperty(i) ) { fo = obj[i]; fp = object[i]; type = Object.prototype.isType.call(fo); if( type === "array" || type === "object" ) //如果是对象 object[i] = fo.extends(fp); else object[i] = fo; } } return object; } Object.prototype.isType = function(){ //为null if( this === null ){ return "null" }; //对象判断 {} if(typeof this === "object" && this instanceof Object && this.constructor === Object && this.toString() === "[object Object]" ) return "object"; //判断对象是 [] if(typeof this === "object" && this instanceof Object && this.constructor === Array && this.push && this.push && this.length ) return "array"; //判断对象是 "" 字符串 if(typeof this === "string" && this.subString ) return "string"; //判断对象是 undefined 字符串 if(typeof this === "undefined" ) return "undefined"; //判断对象是 NaN or 数字 if(typeof this === "number" ){ return isNaN(this) ? "nan" : "number"; } //判断对象是 函数 if(typeof this === "function" && this.prototype ){ return "function"; } return null; } Object.prototype.isArray = function(){ return this.isType() === "array"; } Object.prototype.isObject = function(){ return this.isType() === "object"; } Object.prototype.isNull = function(){ return this.isType() === "null"; }
理论上我2个for循环是非常不好的 这里完全可以用一个for解决 但是 因为时间问题我没来得及优化 大家如果有空帮我改改。。 谢谢!~~~