<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type=text/javascript charset=utf-8> function F(){ alert("函数F") var a= 1; return function(){ this.name = a++; alert(this.name); } } var f1 = F();// 函数F f1();//1 f1();//2 var f2 = F();// 函数F f2();//1 f2();//2 var f3 = F();// 函数F var f31 = new f3();//1 alert(f31.name);//1 var f32 = new f3();//2 alert(f32.name);//2 //------------------------------------------------ var f4 = new F();// 函数F f4();//1 f4();//2 var f5 = new F();// 函数F f5();//1 f5();//2 var f6 = new F();//函数F var f61 = new f6();//1 alert(f61.name);//1 var f62 = new f6();//2 alert(f62.name);//2 </script> </head> <body> </body> </html>
外层函数既可以当成函数看也可以当成类看。内部返回的既可以当成函数看也可以当成类看。
var getXHR = function(){
alert(2);
getXHR = function(){
alert(1);
}
}
getXHR();//2
getXHR();//1
getXHR();//1
var getXHR = function(){
var a = 1;
getXHR = function(){
return 2;
}
return a;
}
alert(getXHR());//1
alert(getXHR());//2
alert(getXHR());//2
var getXHR = function(){
var a = 1;
getXHR = function(){
alert(2);
return 2; //只是return了内部函数
}
getXHR();
return a;
}
alert(getXHR());//2 1
var getXHR = function(){
var a = 1;
getXHR = function(){
alert(2);
return 2;
}
(function(){ //不能这么写
alert(3);
return 2;
})(); // is not a function
getXHR();
return a;
}
alert(getXHR());//2 1
function F(){}
var f = function(){}
Function.prototype.method =function(name,fn){
alert(1);
}
f.method();//1
F.method();//1
function F(){}
var f = function(){}
Function.prototype.method =function(name,fn){
this.prototype[name] = fn;
return this;
}
f.method("a",function(){alert("a");});//1
F.method("b",function(){alert("b");});//1
//f.a();//f.a is not a function
f.prototype.a();//a
//F.b();//F.b is not a function
F.prototype.b()//b
new F().b();//b
本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/6890795.html,如需转载请自行联系原作者