?????? ???
???? ?????
???? ?? ???? ??? ??? ????. ?? ??? ?? ???(?? ??)? ??? ??? ???? ????? ??? ??? ???? ?????. . ???? ??:
1. ?? ??? ?? ??? ??? ??? ? ??????.
2. ???? ??? ??? ? ???? ???? ?? ?? ?????.
??? ??? Javascript? ?? ??? ??? ?????. ?, ?? ??? ?? ???? ?? ??? ?? ??? ?????. ?? ??? ?? ??? ?? ?? ??, ???? ? ?? ??? ???? ?? ??? ??? ?? ?? ??? ???? ? ????. ??? ?? ?? ? ??? ?? ???? ?? ?? ???? ???? ???? ?????.
???? ???? ???? ?? ??
? ?? ?? ??
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script type="text/javascript"> function Circle(r) { this.r = r; } Circle.PI = 3.14159; Circle.prototype.area = function() { return Circle.PI * this.r * this.r; } var c = new Circle(1.0); alert(c.area()); </script> </head> <body> </body> </html>
??? ?? ??? ??? ?? ??? ??? ? ?? ??? ??? ????.
? ?? ?? ??
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script type="text/javascript"> var Circle = function() { var obj = new Object(); obj.PI = 3.14159; obj.area = function( r ) { return this.PI * r * r; } return obj; } var c = new Circle(); alert( c.area( 1.0 ) ); </script> </head> <body> </body> </html>
? ?? ??? ??? ???? ??? ??? ??? ???? ????.
? ?? ?? ??
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script type="text/javascript"> var Circle = new Object(); Circle.PI = 3.14159; Circle.Area = function( r ) { return this.PI * r * r; } alert( Circle.Area( 1.0 ) ); </script> </head> <body> </body> </html>
? ??? ?? ? ?????, ? ??? ?? ?? ?? ??? ??? ???? ???? ????.
??? ? ?? ??
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script type="text/javascript"> var Circle={ "PI":3.14159, "area":function(r){ return this.PI * r * r; } }; alert( Circle.area(1.0) ); </script> </head> <body> </body> </html>
? ??? ????? ???? ?? ?????. var obj = {}? ? ??? ???? ????
Javascript ???? ??
?? ???? ???? ?? ?? ? ? ????. ?? ??, ?? ?? ?? ???? ?????? ? ???, ??? ?? ???? ???? ???? ?? ???? ??? ?? ???? ??????.
1. ?? ?? ?? ??
2. ?? ??
3. ?? ??? ? ??
JavaScript ?? ???? ??? ?? ??? ??? ? ????.
?? JavaScript??? ?? ??? ? ?? ??? ???? ? ????.
JavaScript? ?? ??? ?????. ??? ??? ?? ??? ?? ??? ??? ? ????.
? ??? ?? ?? plus()? ?? ??? ??? ??? ???? ? ????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <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? ? ?? ????? ?? ???.
???? ??? ?????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <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 ???? ???? ??? ? ????.
???? ?? ??? ?? ??? ?? ?? ?? ?? ??? ??? ? ?? ?????.