标签:
一:arguments.callee
<!DOCTYPE html> <html> <head> <script type="text/javascript"> /* var facorial=function(num){ if (num<1) { return 1; }else{ return num*facorial(num-1); } }*/ var facorial=function(num){ if (num<1) { return 1; }else{ return num*arguments.callee(num-1);//arguments.callee代表当前函数 } } var another=facorial; facorial=function(){ return 1; } alert(facorial(5)); alert(another(5)); </script> </head> <body> </body> </html>
二:Prototype
<!DOCTYPE html> <html> <head> <script type="text/javascript"> function Father(name,age){ this.name=name; this.age=age; } Father.prototype.sayHi()=function(){ alert("age+"+this.age+",name="+this.name); } //定义子类 function Son(name,age,gender){ this.gender=gender; Father.call(this,name.age); } Son.prototype=new Father();//这种写法是为了防止父类里面有子类的方法 Son.prototype.fungame=function(){}; var f1=new Father("F",29); f1.sayHi(); var s1=new Son("S",true); </script> </head> <body> </body> </html>
四:闭包
<!DOCTYPE html> <html> <head> <script type="text/javascript"> function Closure(){ var capturedVariable="capturedVariable"; return function(){ alert(capturedVariable); }; } var closure=Closure(); closure(); </script> </head> <body> </body> </html>
标签:
原文地址:http://www.cnblogs.com/FJuly/p/4721807.html