typeof operator and Constucor attribute detection _javascript techniques in JavaScript

Source: Internet
Author: User

* #type. js

Copy Code code as follows:

function person (name, age) {
THIS.name = name;
This.age = age;
}
var d = {an: ' object '};
var a = [' Apple ', ' banana '];
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.

Copy Code code 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]}

Visible, there is a difference between using the TypeOf operator and the Constucor property to detect object type return values.

If the variable is an array, the TypeOf operator returns object, and the constructor property returns array;
If the variable is a constructor object, the typeof operator returns object, and the constructor property returns the constructor
Each variable has its Construcor attribute, which provides not only this object, but also what type of object this object is. In summary, the constructor property holds a constructor that points to an object, whether it is a custom or a native type object.

It is important to note that different browsers detect regular expressions for typeof operators differently, IE and Firefox return ' object '.

Well, today's content on the first here, the small partners if there is doubt, just leave a message.

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.