?????? ???
JavaScript ??? ?? ??? ?? ?? ?? ??? ?? ????.
?? ??? ???? ??? ? ????.
?? ??
??? ??? ?? ?? ??? ??? ??? ???? ? ????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>訪問函數(shù)內(nèi)部定義的變量:</p> <button type="button" onclick="myFunction()">查看</button> <p id="demo"></p> <script> function myFunction() { var a = 4; document.getElementById("demo").innerHTML = a * a; } </script> </body> </html>
????? ???? ??? ???.
??? ?? ??? ??? ???? ???? ? ????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>函數(shù)可以訪問定義在函數(shù)外的變量:</p> <button type="button" onclick="myFunction()">點我</button> <p id="demo"></p> <script> var a = 5; function myFunction() { document.getElementById("demo").innerHTML = a * a; } </script> </body> </html>
???? ?????
?? ??? a? ?? ?????.
? ???? ?? ??? ? ??? ????.
?? ??? ???? ?? ????? ?????.
? ??? a? ?? ?????.
?? ??? ?? ??? ??? ?? ???? ??? ? ????. ?? ??? ???? ???? ??? ? ????.
?? ??? ?? ??? ??? ??? ?? ?? ?????. ? ? ??? ???? ?? ??? ??? ??? ????.
Tips: var ???? ???? ?? ??? ???? ?? ??? ?????? ?? ??? ???.
?? ?? ??
?? ??? ??? ?????. ?, ?? ??? ?? JavaScript ????? ?? ?? ????.
?? ??? ??? ??? ?? ????? ?????. ??? ??? ?? ???? ?? ??? ?????. ??? ????? ????? ?? ???? ?????.
??? ???
?? ?? ?? ??? ?? ???? ???? ??? ? ??? ??? ???.
?? ??? ??? ???? ??? ??? ??? ? ????.
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>全局變量計數(shù)。</p> <button type="button" onclick="myFunction()">計數(shù)!</button> <p id="demo">0</p> <script> var counter = 0; function add() { return counter += 1; } function myFunction(){ document.getElementById("demo").innerHTML = add(); } </script> </body> </html>
????? ???? ??? ???
add() ??? ???? ??? ?? ?????.
??? ??? ??? ????. add() ??? ???? ???? ???? ?? ????? ???? ??? ? ??? ????.
?? ??? ???? ???? ??? ???? ??? ??? ?? ??? ? ????.
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>局部變量計數(shù)。</p> <button type="button" onclick="myFunction()">計數(shù)!</button> <p id="demo">0</p> <script> function add() { var counter = 0; return counter += 1; } function myFunction(){ document.getElementById("demo").innerHTML = add(); } </script> </body> </html>
????? ???? ??? ???
? ??? ???? ????. ????? add() ??? ??? ??? ???? 1? ?????.
JavaScript ?? ??? ???? ? ??? ??? ? ????.
JavaScript ?? ??
?? ??? ?? ??? ??? ? ????.
?? JavaScript??? ?? ??? ? ?? ??? ???? ? ????.
JavaScript? ?? ??? ?????. ??? ??? ?? ??? ?? ??? ??? ? ????.
? ??? ?? ?? plus()? ?? ??? ??? ??? ???? ? ????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>局部變量計數(shù)。</p> <p id="demo">0</p> <script> document.getElementById("demo").innerHTML = add(); function add() { var counter = 0; function plus() {counter += 1;} plus(); return counter; } </script> </body> </html
????? ???? ??? ???.
???? plus() ??? ???? ? ??? ??? ??? ??? ? ???? ?? ??.
?? counter = 0? ? ?? ????? ?? ???.
??? ??? ?????.
JavaScript ???
?? ?? ??? ??????? ? ??? ??? ????
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>局部變量計數(shù)。</p> <button type="button" onclick="myFunction()">計數(shù)!</button> <p id="demo">0</p> <script> var add = (function () { var counter = 0; return function () {return counter += 1;} })(); function myFunction(){ document.getElementById("demo").innerHTML = add(); } </script> </body> </html>
????? ???? ??? ???
?? ?
?? add? ?? ?? ??? ?? ?? ?? ?????.
?? ?? ??? ? ?? ?????. ???? 0?? ?????. ?? ???? ?????.
add ??? ??? ??? ? ????. ?? ??? ?? ?? ???? ???? ?? ???? ????? ????.
?? JavaScript ????? ???. ??? ?? ??? ?? ? ?? ????.
???? ?? ??? ??? ???? add ???? ???? ??? ? ????.
Tips: ???? ?? ??? ?? ???? ?? ??? ??? ?? ??? ???? ? ?? ?????.