-typeof: anulaciones método
由于typeof方法存在一些问题,null一个原始值返回object,且没有办法区分出
包装类,所以我们要写一个更好的方法。
En primer lugar, tenemos que ser enumerados por separado nulo, a continuación, escribir typeof devuelve el resultado como "objeto" de situaciones diferentes, hay una gran variedad de objetos, así como el envasado, entonces pensamos de utilizar el método Object.toString para llevar a cabo, y después de volver de los resultados de juicio. Hábilmente crear un nuevo objeto, el nombre del atributo es el resultado del método toString.
function type(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";
}
if (typeof target == "object") {
//数组,对象,包装类用toString()区别
var str = Object.prototype.toString.call(target);
return template[str];
} else {
return typeof target;
}
}
- Una gama de de-énfasis (hash)
El único uso de las propiedades del objeto, el valor asignado a una serie de propiedades de los objetos!
//数组去重,要求在原型链上添加函数编程,利用对象的性质,哈希方法
Array.prototype.unique = function() {
var temp = {},
arr = [],
len = this.length;
for (var i = 0; i < len; i++) {
if (!temp[this[i]]) {
temp[this[i]] = "abc";
arr.push(this[i]);
}
}
return arr;
};