For the type of judgment, JavaScript uses typeof.
Chestnuts:
Console.log (typeof null); Object
Console.log (typeof []); Object
Console.log (typeof {}); Object
Console.log (typeof new Date ()); Object
Console.log (typeof new Object); Object
Console.log (typeof function () {}); function
Console.log (typeof alert); function
Console.log (typeof 1); Number
Console.log (typeof "ABC"); String
Console.log (typeof true); Boolean
As you can see, typeof does not accurately determine each type of data, such as null and array, and so on. Therefore, JavaScript does not recommend using typeof for data types.
So how to make specific judgments?? Take a look at grammar < ( ̄3 ̄) >!
Chestnuts:
Console.log ({}.tostring.call (null)); [Object Null]
console.log ({}.tostring.call ([])); [Object Array]
console.log ({}.tostring.call ({})); [Object Object]
console.log ({}.tostring.call (New Date ()); [Object Date]
console.log ({}.tostring.call (function () {})); [Object Function]
console.log ({}.tostring.call (new object)); [Object Object]
console.log {}.tostring.call (alert); [Object Function]
console.log ({}.tostring.call (1)); [Object number]
Console.log ({}.tostring.call (' abc ')); [Object String]
console.log ({}.tostring.call (true)); [Object Boolean]
Haha, is not a glance Ah!!
So if you're using jquery, you don't have to be so troublesome, you can use the tool method $.type () to judge
Chestnuts:
Console.log ($.type (null)); Null
console.log ($.type ([])); Array
Console.log ($.type ({})); Object
Console.log ($.type (1)); Number
... Not all finished, results and {}.tostring.call (obj); it's the same.
actually {}.tostring.call (obj) is the most important piece of code (⊙o⊙) for the implementation of the $.type () method in jquery. Oh, amazing! Quickly go to the jquery source to look for it ~ ~
The above is a small series for you to bring about JavaScript and jquery type of the full content of the judgment, I hope that we support cloud Habitat Community ~