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.