EXTJS uses forin for Array traversal and trap Classification

Source: Internet
Author: User
Tags javascript array
I. Q: I have the following code in extjs. Can I guess the execution result? Vararr [1, 2, 3]; for (varkinarr) {alert (arr [k]);} 2. Answer: five items will be generated by alert successively: 1, 2, 3, function (B, c) {varathis. length; cc | 0; c + (c & lt; 0 )? A: 0;... I. Q: Is there the following code in extjs? Guess the execution result? Var arr = [1, 2, 3]; for (var k in arr) {alert (arr [k]);} 2. The answer is: five items will be generated by alert successively: 1, 2, 3, function (B, c) {var a = this. length; c = c | 0; c + = (c <0 )? A: 0; for (; c = B) {return c} return-1}, function (B) {var a = this. indexOf (B); if (! =-1) {this. splice (a, 1)} return this} 3. There are two solutions: 1. Use length to traverse: var arr = [1, 2, 3]; var arrLen = arr. length; for (var I = 0; I alert (arr [I]);} 2. Add the judgment condition: var arr = [, 3]; for (var k in arr) {if (arr. hasOwnProperty (k) {alert (arr [k]) ;}} 4. Analysis Reason: Set 0 to 2 alert (arr [k]); change "0" 2 "0" to "0" 2 "alert (k + '0" 2 "0" 2 "0" 2 "+ arr [k]). 0. 2. 0. 2. The value of K is more indexOf and remove than expected. The disadvantages of traversing arrays with for in are exposed again. View the ext-base.js source code you will find ExtJS in JavaScript Array, Date, Function, Number, string and other extensions, the Array Extension code is as follows: Ext. applyIf (Array. prototype, {indexOf: function (B, c) {var a = this. length; c = c | 0; c + = (c <0 )? A: 0; for (; c <a; ++ c) {if (this = B) {return c} return-1}, remove: function (B) {var a = this. indexOf (B); if (! =-1) {this. splice (a, 1)} return this}); Method Description: indexOf: determines whether the specified object exists in the array. If so, the corresponding index is returned, if not,-1 is returned. Remove: deletes a specified object from the 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.