JavaScript typeof data type detection function

Source: Internet
Author: User

Web effects typeof data type detection function

JavaScript is a weakly typed scripting language, and a weak type does not represent no type. As with other programming languages, data in JavaScript is typed, and large can be divided into simple types and complex types.


Simple Type has Number,string,boolean

Complex types have object and function

The following examples can be described:

typeof 1//echo "number"
typeof 1.1//echo "number"
typeof "Hello"//echo "string"
typeof True//echo "Boolean"

typeof new Object ()//Echo "Object"
typeof New Date ()//echo "Object"
typeof New Array ()//echo "Object"

typeof new Function ()//echo "function"
typeof Object//echo "function"
typeof Date//echo "function"
typeof Array//echo "function"
typeof function//echo "function"

Above the code we can roughly see what kind of JavaScript data type can be derived using the TypeOf function, the complex type has only object and function, and the simple type is only three kinds. In fact, this is not accurate, regardless of complex type or simple type, using typeof is not complete, below we will detail point description.

For simple types, "number" can be divided into two kinds of integer and float, the two types in the calculation of multiplication and division, is not the same, this is a bit of nonsense, in fact, I want to say, float type in and int type calculation, there will be many inexplicable problems, is because of the accuracy of float. And for complex types, it's relatively complex, some of the types that JavaScript has built up from the example above we can see that there are date,object,array,function (there should be others, I can't remember when I write this), although they use typeof In the calculation, only the object can return two kinds of function, but they are not the same, how different, there is no need to repeat. But we can also see from the example above that the object and function two types are more special, and I call them the underlying type. There is a simple method of distinguishing between the types of object that are usually created by new, and the function-critical customization is the type of the role, but the exception is the new function, which creates a function. This may be mentioned later in the definition of the function. The type of object, which is roughly understandable, and what is easy to doubt is the function type, which, when described as a type, should actually be described as an uppercase function. The type of the function itself is a function, and the function type created by the function type (including the use of the Function keyword definition) is also a function. This sentence needs to be carefully understood. However, there is also a point where the defined function can be instantiated (the word may not be appropriate here, but easy to understand), as follows:

function Functiontype ()
{
echo I am a function
}
var func = new Functiontype ();
We use typeof to check the types of Functiontype and Func respectively:

typeof Functiontype//echo "function"
typeof func//Echo "Object"
Again, the type of the defined function is also a function, and the defined function can also be used for instantiation, just like the date,array of the system, which shows that a custom function can be used as a type. This finds a way of customizing the type, which we call a class in object-oriented languages. Now in JavaScript, we also have the definition class method, which defines function. The class is also an object, and the type of the class object is a function.

As you can see, the complex type in JavaScript is actually made up of date,array,object,function, and our custom class (type).

Since there are types, we need to distinguish between types, and as we said above, TypeOf can't exactly differentiate, so we introduce another keyword instanceof, not a function, but it returns a Boolean value that indicates whether an object is an instance of another object. As follows


typeof new Date () instanceof date//echo True
typeof new Array () instanceof Array//echo True
typeof new Object () Instanceof object//echo True

function MyType () {
This are my type
}
var instance = new MyType ();
Instance instanceof MyType ()//echo True
MyType instanceof Function//echo True
New Date () instanceof MyType ()//Echo False

We can see that instanceof is used to check whether an object is an instance of another object. A careful friend may also try this:

function instanceof Function//echo true
Function instanceof Object//echo True
Object Instanceof Object//echo True
Object instanceof Function//true
To be honest, this code is maddening. I also said in the front, object and function are basic types, we also see the relationship between the two when using TypeOf, TypeOf object result is function, I think, This shows that all of the complex types in JavaScript are based on function, in other words, all types are implemented based on function. On the other hand, any object using the Instanceof object result is true, indicating that any object is an instance of objects. The preceding sentence focuses on the type, and then on the instance (function we can also think of it as an instance of ourselves).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.