Home >Web Front-end >JS Tutorial >Javascript determines the specific class reprint of object_javascript skills

Javascript determines the specific class reprint of object_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:19:571026browse

Everyone knows that Javascript's typeof can get the type of a variable, but typeof has only six return values: "number," "string," "boolean," "object," "function," and "undefined."

In fact, Javascript also has many special categories such as Array and Date. Why can't they be returned in typeof?
It turns out that Javascript classifies Array, Date Object, etc. into the object class. We can only use instanceof to do it. Determine the accurate category of object.

Here is a simple example to illustrate the usage of instanceof

Copy code The code is as follows:

function objTest(obj){
var i, t, s = ""; // Create variables.
t = new Array(); // Create an array.
t["Date"] = Date; // Fill in 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 not an instance of " i "n" ;
}

var obj = new Date();
response.write(objTest(obj));

Statement:
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