JS 常用功能函式封裝,繼承、陣列去重、判斷資料型別
阿新 • • 發佈:2018-12-14
繼承方法封裝:
var inherit =(function(){
var F = function(){};
return function(a,b){
F.prototype = b.prototype;
a.prototype = new F();
a.prototype.constuctor = b;
a.prototype.uber = b.prototype;
}
}());
判斷資料型別:
function type (target){ var ret = typeof(target); var template = { "[object Array]" : "array", "[object Object]" : "object", "[object Number]" : "Number-object", "[object Boolean]" : "boolean-object", "[object Steing]" : "string-object" } if(ret == null){ return "null" } else if(ret == 'object'){ var str = Object.prototype.toString.call(target); return template[str]; }else{ return ret } }
陣列去重:
var arr = [1,1,1,2,2,3,4,5,5,7,7,8,8,9]
var arr1 = []
Array.prototype.unique = function (){
var temp = {},
len = this.length;
for(var i = 0 ;i <len; i++){
if (!temp[this[i]]) {
temp[this[i]] = "abc";
arr1.push(this[i])
}
}
return arr1;
}
注:純屬個人瞎寫,有意見請提出討論,不喜勿噴。