A piece of JavaScript code depressing me for half a day

Source: Internet
Author: User

Bin (to write to me) original blog (http://blog.csdn.net/binbinxyz), reprint please indicate the source!

When I was writing something in JS, I encountered a magical phenomenon: directly calling a function a (which is convenient for discussion) can run normally, and calling this function a in another function B has always failed. I declare that function B has nothing to do with function. First paste the Code as follows:

Code 1:

<! Doctype HTML> <body> <SCRIPT type = "text/JavaScript"> function circle (r) {This. R = r;} circle. prototype. area = function () {return 3.14 * This. R * This. r;} var A = new circle (1.0); // alert ("A:" +. area (); function Init () {// some code ...}VaR Circle = {Pi: 3.14159 };Init (); </SCRIPT> </body> 

Code 2:

<! Doctype HTML> <body> <SCRIPT type = "text/JavaScript"> function circle (r) {This. R = r;} circle. prototype. area = function () {return 3.14 * This. R * This. r;} var A = new circle (1.0); // alert ("A:" +. area (); function Init () {// some code... vaR B = new circle (1.0); // The Error alert ("B:" + B. area ());}VaR Circle = {Pi: 3.14159 };Init (); </SCRIPT> </body> 

Check again, debug and debug again. The problem still does not find the cause.

Finally, when I was almost disappointed, I opened the DOM tree structure randomly. The result was highlighted. I found the following code:

var Circle = {  PI: 3.14159};

It turns out that the previously written code has been forgotten to be deleted. Therefore, when the code is run, the result is normal, and when Code 2 is run, an error occurs.

Remember: A function can be defined repeatedly in Javascript, but they have different survival times.

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.