Javascript typeof operator and constucor attribute detection _ javascript skills

Source: Internet
Author: User
This article mainly introduces information about the typeof operator and constucor attribute detection in javascript. For more information, see * # type. js.

The code is as follows:


Function Person (name, age ){
This. name = name;
This. age = age;
}
Var d = {an: 'object '};
Var a = ['apple', 'bana'];
Var f = function (){};
Var s = 'David ';
Var n = 33;
Var B = true;
Var o = new Object ();
Var person = new Person ('Mark', 22 );
Console. log (typeof (d) + ':' + d. constructor );
Console. log (typeof (a) + ':' + a. constructor );
Console. log (typeof (f) + ':' + f. constructor );
Console. log (typeof (s) + ':' + s. constructor );
Console. log (typeof (n) + ':' + n. constructor );
Console. log (typeof (B) + ':' + B. constructor );
Console. log (typeof (o) + ':' + o. constructor );
Console. log (typeof (person) + ':' + person. constructor );

Run $ node type. js

The code is as follows:


Object: function Object () {[native code]}
Object: function Array () {[native code]}
Function: function Function () {[native code]}
String: function String () {[native code]}
Number: function Number () {[native code]}
Boolean: function Boolean () {[native code]}
Object: function Object () {[native code]}
Object: function Person () {[native code]}

It can be seen that there are differences between using the typeof operator and the constucor attribute to detect the return values of object types.

If the variable is an Array, the typeof operator returns the object, and the constructor attribute returns the Array;
If the variable is a constructor object, the typeof operator returns the object, and the constructor attribute returns the constructor.
Each variable has its construcor attribute. this attribute not only provides the object or not, but also provides the object type. in short, the constructor attribute stores a constructor pointing to an object, whether it is a custom object or a native object.

One thing to note is that different browsers will have different regular expressions for the typeof operator detection, and IE and Firefox will return 'object '.

Well, today's content is here first. if you have any questions, leave a message below.

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.