js detection data type three ways

1、typeof

typeof to acquire a type of variable or expression, typeof returns the following general only a few results:

typeof "John"                  // return String 
typeof 3.14                    // return Number 
typeof NaN3                     // returns Number 
typeof  to false                   // returns the Boolean 
typeof [1,2,3,4]               // Returns Object 
typeof {name: 'John', Age: 34}   // returns Object 
typeof  new new a Date ()              // returns Object 
typeof  function () {}          // return function 
typeof myCar                   @Return undefined (if myCar not declared) 
typeof  null                    // return object

Please note:

  • The number of data types is NaN
  • Array (the Array) type data object
  • Date (Date) type data is object
  • The object data type is null
  • Data type is undefined undefined variables

If the object is a JavaScript Array or JavaScript Date, we can not pass  typeof  to determine their type, as are the return object.

 

We can use to get a typeof variable exists, such as  if (typeof a! = "Undefined ") {}, rather than going to use the IF (a) because if a is not present (not declared) error occurs.

Because typeof encounter null, array, when objects are returned object type, so when we have to determine whether an object is an array.

Or to determine whether a variable is an instance of an object will have to choose another key syntax  instanceof.

 

2、instanceof

The object can be determined by the particular type instanceof operator, syntax:

var result = objectName instanceof objectType

Returns a Boolean value and returns true if the specified type, otherwise it returns false:

Example:

= ARR [l, 2,3 ];
 IF (ARR the instanceof the Array) { 
    document.write ( "an array ARR" ); 
} the else { 
    document.write ( "not an array ARR" ); 
}

3, constructor property

constructor  property returns constructor all JavaScript variables.

"John" .constructor                  // return function String () {[Native code]} 
(3.14) .constructor                  // return function Number The () {[Native code]} 
to false .constructor                   // return function Boolean () {[native code ]} 
[1,2,3,4] .constructor               // function that returns the Array () {[Native code]} 
{name: 'John', Age: 34 is .constructor}   // return function Object () {[native code ]} 
new new a Date (). constructor              // function returns a Date () {[Native code]} 
function () {} .constructor          // return function function () {[native code] }

You can use the constructor property to see whether the object is an array (containing the string "Array"):

function isArray(myArray) {
    return myArray.constructor.toString().indexOf("Array") > -1;
}

You can use the constructor property to see whether the object is a date (that contains the string "Date"):

function isDate(myDate) {
    return myDate.constructor.toString().indexOf("Date") > -1;
}

Guess you like

Origin www.cnblogs.com/art-poet/p/12098075.html