Js data type judgment and array judgment, js data type array

Source: Internet
Author: User

Js data type judgment and array judgment, js data type array

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

String: It is described by single or double quotation marks, such as "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

Typeof for data type determination

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, so 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?

Method 1 for js to determine array types instanceof

Instanceof is used to determine whether a variable is an instance of an object. It is a three-object computing formula-essentially different from typeof.

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

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

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

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);

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 3 feature profiling

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.

var a = new Array("apple", "banana", "cactus");document.write(a.propertyIsEnumerable(1));
Method 4: the simplest method

For this method, the following links are provided for reference:

Http://blog.csdn.net/zhangw428/article/details/4171630

Http://my.oschina.net/sfm/blog/33197

Http://openxtiger.iteye.com/blog/1893378

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

Of course, you know, this article is not entirely from my own hands, so I still need to list it in reference articles:

Http://www.2fz1.com /? P = 277

Http://msdn.microsoft.com/zh-tw/library/adebfyya.aspx

Http://blog.sina.com.cn/s/blog_532751d90100iv1r.html

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.