How to clear the Javascript array [reprint]

Source: Internet
Author: User

 

This is a very basic problem, but we often do wrong. When an array needs to be cleared, many people use the following method: A = []; we know that the Javascript variable storage method is divided into reference type and direct quantity. The array belongs to the object, that is, the reference type. It references the variable pointer address, which is designed to save memory. In addition, the preceding empty array method is used. If a new array is assigned directly, the referenced array may not be released (with other references). For exampleCode: Var A = [2, 3]; var B = A; A = []; console. log (B); at this time, A and B are not the same array. Clearing a and B still refer to the previous reference address. Unless you intentionally do, this will pose a risk. Therefore, the best way to empty the array is to set the length to 0, that is, a. Length = 0; [reprint address: http://js8.in/1116.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.