26. Class array conversions to arrays

Source: Internet
Author: User

There are three types of class array objects:


2. Elements Collection

3. Node Collection

var olis = document.getelementbytagname ("div");

Console.dir (Olis); An instance of the->htmlcollection element collection class, which is also a collection of class arrays

var olis = document.getelementbyname ("div");

Console.dir (Olis); An instance of the->nodelist node collection class, which is also a collection of class arrays

Class array conversions to arrays:

-"Standard Browser method:

var ary = [].slice.call (Olis)

Problem: Busy in Ie6-8 browser does not support the slice implementation of the borrowed array to convert an array of elements of an element collection to an error (element collection and node collection are not supported, but arguments is supported without any compatibility issues)

-"non-standard method; ie6-8

for (var i = 0;i<olis.length;i++) {

Ary[ary.length] = Olis[i]


Complementary points of knowledge:

1. Browser exception information capture:

We use Try,catch in JS to capture the exception information of the browser

Console.log (num)//Direct error, in JS, the bank error, the following code is not executed

Console.log ("OK")

1) If the exception information is captured with Try,catch, the following code is not affected to continue execution. If the code in the try executes an error, the code in the catch is executed by default

 Trycatch(e) {//  parameter must be written, usually named E  console.log ("error") Console.log ( e.message,111111)//can collect the cause of the current Code error} console.log ("OK")


Requirements: Sometimes you want to capture the error message, and you don't want the following code to continue

  Try {  //  JS code Catch  (e) {  //  If code error executes code in catch
Manually throwing an error message, terminating code execution
throw new Error ("The current network is busy, please try again later")
New Referenceerror//-Reference error message
New TypeError//Type Error
New Rangeerror//Range error
finally { // do not use: Execute code in Finally, regardless of whether the code in the try is an error or not

A complete array of classes, a way to convert to arrays

varUtils = {    //implements transforming an array of classes into arrays, using Try,catch to detect compatibilitylisttoarray:function (likeary) {varary = []; Try{ary=Array.prototype.slice.call (likeary); } Catch(e) { for(vari =0; i<likeary.length;i++) {Ary[ary.length]=Likeary[i]}} returnary; }}

26. Class array conversions to arrays

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.