JS原生代码封装数组去重

1.数组原生链上数组去重

Array.prototype.unique = function (){
			
			var map = {},
				arr = [],
				len = this.length;
			for(var i = 0 ; i < len ; i ++ )
			{
				if(!map[this[i]])
				{
					map[this[i]] = "abc";
					arr.push(this[i]);
				}
			}

			return arr;
		}

2.获得目标的类型

function type (target){
			var targetType = typeof(target);
			var template = {
				"[object Array]":"Array",
				"[object Object]":"Object",
				"[object Number]":"Number-Object",
				"[object Boolean]":"Boolean-Object",
				"[object String]":"String-Object"
			};
			if(target === null)
			{
				return "null";
			}
			else if(targetType == "object")
			{
				var str = Object.prototype.toString.call(target);
				return template[str];
			}else{
				return targetType;
			}
			
		}

3.圣杯模式继承


function inherit(Target, Origin){
			function F(){};
			F.prototype = Origin.prototype;
			Target.prototype = new F();
			Target.prototype.constructor = Target;
			Target.prototype.uber = Origin.prototype;
			
		}

4.深层克隆

function deepClone(target, origin){
			var tar = target || {};
			var arrayType = "[object Array]";
			 for(var prep in origin)
			 {
				 if(origin.hasOwnProperty(prep)){
					  if(typeof(origin[prep]) === "object")
					  {
						  target[prep] = Object.prototype.toString.call(origin[prep]) === arrayType ? [] : {};
						  deepClone(target[prep], origin[prep])
					  }else{
						 target[prep] = origin[prep]; 
					  }
				 }
			 }
			return tar;
		}

你可能感兴趣的:(JS)