Home > Web Front-end > JS Tutorial > body text

The difference between javascript instanceof and typeof_javascript skills

WBOY
Release: 2016-05-16 18:31:28
Original
949 people have browsed it

Why is the result false?

Copy code The code is as follows:



You have to distinguish the difference between string and String
aColors[0] is a string value type, of course not an instance of String La. Refer to the following code
var aColors = ["red", "green", "blue"];
aColors[0]= new String("1")
alert(typeof aColors[0]); //output "Object"
alert(aColors[0] instanceof String); //output "true";

For more information, please refer to the following article:

instanceof Operators
return a Boolean value indicating whether the object is an instance of a specific class.
result = object instanceof class
Parameters
result
Required. Any variable.
object
Required. Any object expression.
class
Required. Any defined object class.
Description
The instanceof operator returns true if object is an instance of class. Returns false if object is not an instance of the specified class, or if object is null.
Example
The following example illustrates the usage of instanceof operator.
Copy code The code is as follows:

function objTest(obj){
var i, t, s = ""; // Create variable.
t = new Array(); // Create an array.
t["Date"] = Date; // Fill the array.
t["Object"] = Object;
t["Array"] = Array;
for (i in t)
{
if (obj instanceof t[i]) / / Check the class of obj.
{
s = "obj is an instance of " i "n";
}
else
{
s = "obj is not an instance of " i "n" ;
}
}
return(s); // Return a string.
}
var obj = new Date();
document.write(objTest(obj));

Instanceof and typeof can both be used to determine whether a variable is empty Or what type of variable it is.
Typeof is used to obtain the type of a variable. Typeof generally can only return the following results: number, boolean, string, function, object, undefined. We can use typeof to get whether a variable exists, such as if(typeof a!="undefined"){}, instead of using if(a) because if a does not exist (undeclared), an error will occur. For Array, Null When using typeof for special objects, object will always be returned. This is the limitation of typeof.
If we want to get whether an object is an array, or determine whether a variable is an instance of an object, we must use instanceof. instanceof is used to determine whether a variable is an instance of an object. For example, var a=new Array(); alert(a instanceof Array); will return true, and alert(a instanceof Object) will also return true; this is because Array is subclass of object. Another example: function test(){};var a=new test();alert(a instanceof test) will return true.
When it comes to instanceof, we have to insert one more problem, which is the arguments of function. We may all think that arguments are an Array, but if you use instanceof to test, you will find that arguments are not an Array object, even though it looks similar.
Also:
Test var a=new Array();if (a instanceof Object) alert('Y');else alert('N');
Get 'Y'
But if (window instanceof Object) alert('Y');else alert('N');
Get'N'
So, the object tested by instanceof here refers to the object in js syntax, not the dom model object.
There will be some differences when using typeof
alert(typeof(window) will get object
When you are young, you should talk less and do more.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!