typeof method rewrites array deduplication


-typeof: method overrides


	由于typeof方法存在一些问题,null一个原始值返回object,且没有办法区分出
包装类,所以我们要写一个更好的方法。

First, we have to be listed separately null, then write typeof returns the result as "object" of different situations, there is an array of objects as well as packaging, then we think of using the method Object.prototype.toString to perform, and then returned from the results of judgment. Skillfully create a new object, attribute name is the result of toString method.

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;
            }
        }

- An array of de-emphasis (hash)


The only use of the properties of the object, the value assigned to an array of object properties!
Here Insert Picture Description

//数组去重,要求在原型链上添加函数编程,利用对象的性质,哈希方法
        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;
        };
Published 37 original articles · won praise 0 · Views 695

Guess you like

Origin blog.csdn.net/weixin_43704007/article/details/105144404