How does Javascript determine the data type and array type, and javascript data type?

Source: Internet
Author: User

How does Javascript determine the data type and array type, and javascript data type?

Such basic things should not be recorded any more. However, let's learn more ~ Let's start with the data type.

Js six data types: number, string, object, Boolean, null, undefined

String: single or double quotation marks, for example, "string"

Number: What integers? floating point numbers are all numbers, you know ~

Boolean: true or false.

Undefined: undefined. It means you didn't assign a value to a variable after creating it ~

Null

Object: This is also hard to explain. Is the types except the above five

------------------ The above are all floating clouds, and the following is the Shenma ------------------------------

Determination of Data TypesTypeof

Typeof can be used to determine most data types. It is a one-dimensional operation. Before an operation value, the return value is a string. This string indicates the type of the Operation number, therefore, to determine whether a data type is String, you can directly if (typeof (your value) = "string "){}

The following are the returned results of various data types:

Var a = "string"; console. log (a); // stringvar a = 1; console. log (a); // numbervar a = false; console. log (a); // booleanvar a; console. log (typeof a); // undfinedvar a = null; console. log (typeof a); // objectvar a = document; console. log (typeof a); // objectvar a = []; console. log (a); // objectvar a = function () {}; console. log (typeof a) // function can be used to determine the data type and function type.

In this way, it is obvious that, except for the first four types, null, objects, and arrays all return object types;

Function is returned for the function type, such as typeof (Date) and typeof (eval.

Then, we can extend another hot topic and solve the common problem. How can we determine that the data is an array type?

--------------------------------------- In fact, this is my purpose ~ ----------------------------------------------

Js Method for Determining the array type

Method 1Instanceof

Instanceof is used to determine whether a variable is an instance of an object. It is a three-object computing formula --- the most substantial difference

A instanceof B? Alert ("true"): alert ("false") // note that the value of B is the data type you want to determine, not a string, such as Array

Example:

Var a = []; console. log (a instanceof Array) // return true

Method 2Constructor

Definition in W3C definition: the constructor attribute returns a reference to the array function that creates this object.

Returns the constructor corresponding to the object. In terms of definition, it is not consistent with instanceof, but the effect is the same.

For example, (a instanceof Array) // is a Array instance? True or false

(A. constructor = Array) // is the constructor of instance a Array? True or false

Example:

Function employee (name, job, born) {this. name = name; this. job = job; this. born = born;} var bill = new employee ("Bill Gates", "Engineer", 1985); console. log (bill. constructor); // output function employee (name, jobtitle, born) {this. name = name; this. jobtitle = job; this. born = born ;}

The following methods are used to determine various types of data:

console.log([].constructor == Array);console.log({}.constructor == Object);console.log("string".constructor == String);console.log((123).constructor == Number);console.log(true.constructor == Boolean);

------------------------------------- The following is not original --------------------------------------

More rigorous and common methods:

function isArray(object){ return object && typeof object==='object' &&   Array == object.constructor;}

!! Note:

Using instaceof and construcor, the determined array must be declared on the current page! For example, a page (parent page) has a framework that references a page (Child page) and declares an array in the Child page, assign the value to a variable on the parent page. Then, the variable is determined. Array = object. constructor; returns false;

Cause:

1. array is a type of referenced data. In the transfer process, only the reference address is transmitted.
2. The address referenced by the Array native object on each page is different. The constructor corresponding to the array declared on the subpage is the Array object of the subpage; the parent page is used for judgment. The Array used is not equal to the Array of the Child page. Remember, otherwise it is difficult to track the problem!

Method 3Feature profiling method

The above methods have some shortcomings, but we must believe that the wisdom of the masses is omnipotent. We can judge their types based on some characteristics of the array.

Function isArray (object) {return object & typeof object === 'object' & typeof object. length = 'number' & typeof object. splice === 'function' & // determines whether the length attribute is enumerable. If the array is set to false! (Object. propertyIsEnumerable ('length '));}

Length and splice are not necessarily arrays, because attributes can be added to an object, but length attributes cannot be enumerated, which is the most important judgment factor.

Ps: The propertyIsEnumerable method is widely used here:

Object. propertyIsEnumerable (proName)

Determines whether a specified attribute can be listed

Note: If the proName exists in the object, you can use a... If the In loop is exhausted, the propertyIsEnumerable attribute returns true. If the object does not have a specified attribute or the specified attribute is not enumerable, The propertyIsEnumerable attribute returns false.

The propertyIsEnumerable attribute does not consider objects in the prototype chain.

Example:

var a = new Array("apple", "banana", "cactus");document.write(a.propertyIsEnumerable(1));

Method 4The simplest method

function isArray(o) { return Object.prototype.toString.call(o) === ‘[object Array]‘;}

The above is all about the content of this article. For more information about the JavaScript syntax, see: JavaScript reference tutorial and JavaScript code style guide. I also hope you can provide more support.

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.