JSコードパッケージ配列の重さ
11213 ワード
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;
}