JavaScript check-in data types

 

 

  • typeof: for detecting the data type of operator.

    Data types can be returned six species ( "number", "string", "bolean", "undefined", "function", "object"). In the js, null value indicates a null object pointer, by detecting typeof returns null "object".

  • instanceof: detecting whether a particular instance of a class.

    Objects as long as the current instance of the prototype chain, with which are detected to true. In the prototype class inheritance, the final result may not be detected correctly.

  • constructor: the attribute is a function of the prototype, the property itself points to the constructor.

    And instsnceof effect very similar to instanceof is different, not only can handle reference data types, and to process the original data type. When rewriting the prototype of a class, in the process of rewriting before the constructor is likely to give cover, this detected result is inaccurate.

  • Object.prototype.toString.call (): toString Object method on an object prototype chain.

    console.log(Object.prototype.toString.call(1));          //[object Number]
      console.log(Object.prototype.toString.call(/^sf/));        //[object RegExp]
      console.log(Object.prototype.toString.call("hello"));      //[object String]
      console.log(Object.prototype.toString.call(true));        //[object Boolean]
      console.log(Object.prototype.toString.call(null));        //[object Null]
      console.log(Object.prototype.toString.call(undefined));      //[object Undefined]
      console.log(Object.prototype.toString.call(function() {}));    //[object Function]
      console.log(typeof(Object.prototype.toString.call(function() {})));    //string

     

Guess you like

Origin www.cnblogs.com/wangshouren/p/11615826.html