JavaScript ECMAScript type judgment

Source: Internet
Author: User

The variables in JS are loosely typed (i.e., weakly typed) and can be used to hold any type of data.

typeof The unary operation to determine whether a primitive type or an object type is used

typeof can be used to detect the data type of a given variable, the possible return value:

1. ' Undefined '---this value is undefined;

2. ' Boolean '---This value is a Boolean value;

3. ' String '---This value is a string;

4. ' Number '---this value is numeric;

5. ' Object '---This value is an object or null;

6. The ' function '---this value is a functional.

var New Array (3);d ocument.write (typeof a);  // undefineddocument.write (typeof//  Object

However, TypeOf is limited in its ability to return "object" for the date, RegExp type. Such as:

typeof //  typeof//typeofnew//

instanceof judgment is that type the left operator must be an object

var New Array (3instanceof array);  // true

This can be used to determine whether an object is an array type, and the IsArray () method, which can be used to directly determine whether a group

var New Array (3); Aa.isarray ();

Constructor property

The constructor property returns a reference to the array function that created this object.

In the JavaScript language, the constructor property is specifically designed for function, and it exists in the prototype attribute of each function. This constructor holds a reference to the function. When defining a function (as shown in the code below),
function F () {//Some code}
? The following actions are performed inside JavaScript: adding a prototype (i.e., prototype) property to the function
? Add an extra constructor property for the prototype object, and this property holds a reference to the function f
So when we create an object with function f as a custom constructor, the object instance automatically saves a property of the prototype object that points to its constructor (here is our custom constructor f) __proto__, So we can access all of the properties and methods owned by the constructor's prototype in each object instance as if they were instances of their own. Of course, this instance also has a constructor attribute (obtained from prototype), when constructor's role is obvious, because at this point, each object instance can access its constructor through the Constrcutor object,

var New Array (3); ary[0] = "1";ary[1] = "1"; ary[2] = "1"; ary[3] = "1"; ary[4] = "1" c7>= = Array);    

JavaScript ECMAScript type judgment

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.