In JavaScript, typeof and instanceof are often used to determine whether a variable is null or of any type. But there is a difference between them:
Typeof
Typeof is a one-dimensional operation. Before a single operation, the operation can be of any type.
The return value is a string that represents the type of the number of operations. Typeof generally only returns the following results:
Number, Boolean, String, function, object, undefined. We can use typeof to obtain whether a variable exists, such as if (typeof! = "Undefined") {alert ("OK")}, instead of using if (a) because if a does not exist (not declared), an error will occur. For array, null and other special objects use typeof to return all objects, which is exactly
Limitations of typeof.
A small example on the Internet:
Instanceof
Instance: instance, for example
A instanceof B? Alert ("true"): Alert ("false"); // is a B instance? True: false
Instanceof is an instance used to determine whether a variable is an object, such as var a = new array (); alert (A instanceof array); true is returned, and alert (A instanceof object) true is also returned because array is a subclass of the object. For example, function test () {}; var A = new test (); alert (A instanceof test) will return
When talking about instanceof, We need to insert another question: function arguments. We may all think that arguments is an array. However, if we use instaceof to test, we will find that arguments is not an array object, although it looks very similar.
In addition:
Test var A = new array (); if (a instanceof object) Alert ('y'); else alert ('n ');
Get 'y'
However, if (window instanceof object) Alert ('y'); else alert ('n ');
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Script language = "JavaScript" type = "text/JavaScript">
Document. Write ("typeof (1):" + typeof (1) + "<br> ");
Document. Write ("typeof (NAN):" + typeof (NAN) + "<br> ");
Document. Write ("typeof (number. min_value):" + typeof (number. min_value) + "<br> ");
Document. Write ("typeof (infinity):" + typeof (infinity) + "<br> ");
Document. Write ("typeof (\" 123 \ "):" + typeof ("123") + "<br> ");
Document. Write ("typeof (true):" + typeof (true) + "<br> ");
Document. Write ("typeof (window):" + typeof (window) + "<br> ");
Document. Write ("typeof (Array ():" + typeof (new array () + "<br> ");
Document. Write ("typeof (function () {}):" + typeof (function () {}) + "<br> ");
Document. Write ("typeof (document):" + typeof (document) + "<br> ");
Document. Write ("typeof (null):" + typeof (null) + "<br> ");
Document. Write ("typeof (eval):" + typeof (eval) + "<br> ");
Document. Write ("typeof (date):" + typeof (date) + "<br> ");
Document. Write ("typeof (SSS):" + typeof (SSS) + "<br> ");
Document. Write ("typeof (undefined):" + typeof (undefined) + "<br> ")
</SCRIPT>
<Title> JavaScript '² ² annotation </title>
</Head>
<Body>
</Body>
</Html>
Typeof (1): Number
Typeof (NAN): Number
Typeof (number. min_value): Number
Typeof (infinity): Number
Typeof ("123"): String
Typeof (true): Boolean
Typeof (window): Object
Typeof (Array (): Object
Typeof (function () {}): Function
Typeof (document): Object
Typeof (null): Object
Typeof (eval): Function
Typeof (date): Function
Typeof (SSS): Undefined
Typeof (undefined): Undefined
'N'
Therefore, the instanceof object here refers to the object in JS syntax, not the DOM model object.
There are some differences when typeof is used.
Alert (typeof (window) will get the object