常用的代码集合 简单单调的重复工作

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

Object.prototype.each = function(Callback){

    for( var i in this ){
      if ( i !== "length" && this.isArray() ) return ; 
      Callback.apply(this,[i,this[i]]);
    }
      
}


  以后还会根据情况增加  不做重复工作的目标。

posted @ 2017-10-19 11:15  blurs  阅读(178)  评论(0编辑  收藏  举报