The JavaScript function has the same name overlay problem

Source: Internet
Author: User
Tags network function

In JavaScript scripts, local functions have the same name as the perimeter function, which overrides the out-of-network function, where variables can be defined repeatedly.

See the example below.

A =  function () {var me = This;me.method1 = function () {var items = [1,2,3,4,5];for (var i=0;i<items.length;i++) {if (1 {var items = [6,7,8];if (items.length = = 0) {alert (' Test is ok! ');} Alert (Items[i]);}}}
The local variables items are defined as follows:

var items = [5,6,7];

The definition of the extranet variable will be overwritten:

var items=[1,2,3,4,5];

Loops can only be performed 3 times.

The workaround is to use a different function name to avoid the same name as the function.

The JavaScript function has the same name overlay problem

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.