Js accurately determine the type of the object - and on the difference between typeof method Object.prototype.toString

Typeof may be used to determine the data in javascript type, but typeof Analyzing only distinguish basic types, number, string, boolean, undefinded and five types of object ;

        <script type="text/javascript">
            //基本数据类型:number、string、boolean、null、undefined
            //复杂数据类型(引用数据类型):object(Array、Data、RegExp、function)
            alert(typeof 1);  //number;
            alert(typeof true);  //boolean;
            alert(typeof undefined);  //undefined;
            alert(typeof "hello world")   //string;

            alert(typeof {});  //object;
            alert(typeof null); //object,  null是一个空对象;
            alert(typeof function(){});   //function;
        </script>

As described above: for the null (empty object), an array {} (object) is determined using typeof data types, are returned unified "object" string ;


To accurately determine the type of the object may be used in js Object.prototype.toString method (determination target objects belonging to that type of built-in);

eg:

var arr=[];
console.log(Object.prototype.toString.call(arr));  //结果是  "[object Array]"

In ES3, the in is invoked, performs the following method Object.prototype.toString Procedure:
1. Obtain this object [[Class]] value of the property;
2. calculate "[object" + "of a step of acquiring the attribute value "+"] "new string after three strings together;
3. step 2 returns the calculated new character string;

The process simply is:
1. The name of the class (object type) acquisition targets.
2. Then [Object, obtaining object type name] is a combination of string
returns the string "[the Array Object]" 3. .

[[Class]] is an internal attribute, all objects (native object and host object) owns the property in the specification, [[Class]] is so defined:
internal properties, [[Class]] a string value indicates the type of the object .


Because everything is an object in JavaScript, not any exception () method was as follows for all types of applications values ​​Object.prototype.toString.call:

        <script type="text/javascript">
            console.log(Object.prototype.toString.call(123))    //"[object Number]"
            console.log(Object.prototype.toString.call('123'))    //"[object String]"
            console.log(Object.prototype.toString.call(undefined))    //"[object Undefined]"
            console.log(Object.prototype.toString.call(true))    //"[object Boolean]"
            console.log(Object.prototype.toString.call(null))    //"[object Null]"
            console.log(Object.prototype.toString.call({}))    //"[object Object]"
            console.log(Object.prototype.toString.call([]))    //"[object Array]"
            console.log(Object.prototype.toString.call(function(){}))    //"[object Function]"
        </script>

As can be seen: Object.prototype.toString.call () can be accurately determined object type;


Determine whether the function

   function isFunction(it) {
        return Object.prototype.toString.call(it) === '[object Function]';
    }

There are compatibility issues, comprehensive wording is:

 function isArray(arr){  //自己封装的一个函数isArray(),用于判断函数传入的参数是否是数组;
                if(typeof Array.isArray === "undefined"){  //Array.isArray()是ES5中新增的方法,先判断下其是否是undefined,如果是未定义的话执行该段函数,定义一个Array.isArray()方法;
                    Array.isArray = function(brr){  //如果未定义,则定义函数并传入参数;
                        return Object.prototype.toString.call(brr)=="[object Array]"  //通过js中的Object.prototype.toString方法,返回一个判断,假设参数对象值属于数组内置类型,判断时,如果返回true则是数组,否则不是;
                    }
                }
                return Array.isArray(arr);  //返回该方法,调用isArray()相当于调用Array.isArray();
            }

As the array is determined, similar to the other is determined;

Published 26 original articles · won praise 3 · Views 7954

Guess you like

Origin blog.csdn.net/Eva3288/article/details/52781128