?????? ???
JavaScript ???
???? ?????
???? ?? ???? ??? ??? ????. ?? ??? ?? ???(?? ??)? ??? ??? ????? ????? ??? ??? ?????. ???. ???? ??:
1. ?? ??? ?? ??? ?? ?? ? ??????.
2. ???? ??? ??? ? ???? ???? ?? ?? ?????.
??? ??? Javascript? ?? ??? ??? ?????. ?, ?? ??? ?? ???? ?? ??? ?? ??? ?????. ?? ??? ?? ??? ?? ?? ??, ???? ? ?? ??? ???? ?? ??? ??? ?? ?? ??? ???? ? ????. ??? ?? ?? ? ??? ?? ???? ?? ?? ???? ???? ???? ?????.
2. ???? ???? ???? ?? ?? ??
?? JS? ?? ?? ???? ??? ??? ? ????? ?? ???? ???. ?? ???? ???? 5?? ??? ???? ???? ???? ???? ??? ?????. ?? ???? ??? ??? ???????.
//? ?? ?? ??
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());
? ?? ??? ??? ?? ??? ??? ? ?? ??? ??? ????.
//? ?? ?? ??
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 ) );
? ?? ??? ??? ???? ??? ??? ??? ???? ????.
//? ?? ?? ??
var Circle = new Object(); Circle.PI = 3.14159; Circle.Area = function( r ) { return this.PI * r * r; } alert( Circle.Area( 1.0 ) );
? ??? ?? ? ?????, ? ??? ?? ?? ?? ??? ??? ???? ???? ????.
//??? 4?? ??
var Circle={ "PI":3.14159, "area":function(r){ return this.PI * r * r; } }; alert( Circle.area(1.0) );
? ??? ????? ?? ???? ?? ?????. var obj = {}? ? ??? ?????.
//?? ?? ?? ??
var Circle = new Function("this.PI = 3.14159;this.area = function( r ) {return r*r*this.PI;}")
Alert ( (new Circle()).area(1.0) )
??? ?? ? ?? ??? ???? ?? ??? ????? ???.
????? ?? ??? ? 2?? 4?? ? ?? ???, ??? ?? ????? ???.
? ???? JS?? ?? ???? ?????? ???? ?????? ??? ?????? ???????:
var dom = function(){ }; dom.Show = function(){ alert("Show Message"); }; dom.prototype.Display = function(){ alert("Property Message"); }; dom.Display(); //error dom.Show(); var d = new dom(); d.Display(); d.Show(); //error
?? ?? ??
?? ??? ??? ??????. ?, ?? ??? ?? JavaScript ????? ?? ?? ????.
?? ??? ??? ??? ?? ????? ?????. ??? ??? ?? ???? ?? ??? ?????. ??? ????? ????? ?? ???? ?????.
??? ???
? ?? ?? ???? ??? ?? ???? ???? ??? ? ??? ??? ???.
?? ??? ??? ???? ??? ??? ??? ? ????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </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() ??? ???? ???? ???? ?? ????? ???? ??? ? ??? ????.
?? ??? ???? ???? ??? ???? ??? ??? ?? ??? ? ????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </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"> </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? ? ?? ????? ?? ???.
??? ??? ?????.