JavaScript determines whether an object is a Array_ or a basic knowledge

Source: Internet
Author: User
1.typeof operator. For function, String, number, Undefined, and so on, for several types of objects, he is perfectly capable, but for array

Copy Code code as follows:

var arr=new Array ("1", "2", "3", "4", "5");
Alert (typeof (arr));


You'll get an object answer that's a bit disappointing.
2.instanceof operator, the instanceof operator in JavaScript returns a Boolean value that indicates whether an object is an instance of a particular class.
How to use: Result = Object instanceof class, or just the array, again, well, successfully returns TRUE.
Copy Code code as follows:

var arraystr=new Array ("1", "2", "3", "4", "5");
Alert (arraystr instanceof Array);


Short summary: It seems that the problem we are discussing today has been answered, but in fact the shuttle in multiple frame will have a big problem.

Code
Copy Code code as follows:

var iframe = document.createelement (' iframe ');
Document.body.appendChild (IFRAME);
Xarray = Window.frames[window.frames.length-1]. Array;
var arr = new Xarray ("1", "2", "3", "4", "5");//This is not supported under IE elder brother, FF only
Alert (arr instanceof Array); False
Alert (Arr.constructor = = Array); False

The return result is two false, disappointing.

ECMA-262 wrote
Object.prototype.toString () when the "toString" is called, the following steps are taken:
1. Get the [[Class]] property of this object.
2. Compute a string value by concatenating the three Strings "[object", result (1), and "]".
3. Return result (2)
The above specification defines the behavior of the Object.prototype.toString: first, get an internal property of an object [[Class]], and then, based on this property, returns a similar to "[Object Array]" string as a result (read the ECMA standard should know that [[]] is used to represent an externally inaccessible property, called an "internal property," used within a language.) Using this method, and then with call, we can get the internal properties [[Class]] of any object, and then convert the type detection into string comparisons to achieve our goal. Let's take a look at the description of array in the ECMA standard:
ECMA-262 wrote
New Array ([item0[, Item1 [,...]])
The [[Class]] property of the newly constructed object is set to "Array".
So using this, the third approach comes in.
function IsArray (obj) {
return Object.prototype.toString.call (obj) = = ' [Object Array] ';
Call Change ToString This refers to the object to be instrumented, returns a string representation of this object, and then compares whether the string is ' [object Array] ' to determine whether it is an instance of Array. Perhaps you have to ask, why not directly o.tostring ()? Well, although the array inherits from object, there will be the ToString method, but this method may be rewritten to meet our requirements, and Object.prototype is the tiger's butt, few people dare to touch it, so to a certain extent to ensure its "purity":
Unlike the previous scenarios, this method is a good solution to the problem of building a cross frame object, after testing, the major browser compatibility is also very good, you can rest assured that the use. The good news is that many frameworks, such as jquery, Base2, and so on, plan to use this method to implement certain types of objects, such as arrays, regular expressions, and so on, without our own writing.

Besides, EXT3 has already replaced it with the same wording.
Copy Code code as follows:

Isarray:function (v) {
return tostring.apply (v) = = ' [Object Array] ';
}
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.