《2018年10月2日》【连续364天】
标题:type实现,数组去重;
内容:
// 实现type
function myType(target){
var type="";
var ret = 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){
type="null";
}else if(ret=="object"){
var str =Object.prototype.toString.call(target);
type=template[str];
}else{
type=ret;
}
return type;
}
// 数组去重
Array.prototype.unique = function () {
var temp = {};
var arr = [];
var len = this.length;
for(var i = 0; i < len ; i++){
if(!temp[this[i]]){
temp[this[i]] = "a";
arr.push(this[i]);
}
}
return arr;
}