<!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(){ this.name = 1; this.sc = 4; alert(2); return function(){ this.age = 2; this.name = 3; alert(1); } } var f = new F(); alert(typeof f);//function alert(f.name);//输出函数的名字 alert(f.age);//undefined alert(f.sc);//undefined function G(){ this.name = 1; this.sc = 4; return { name : 14, age : 15 }; } var g =new G(); alert(typeof g);//object alert(g.name);//14 alert(g.age);//15 alert(g.sc);//undefined function H(){ this.name = 1; this.sc = 4; } var g =new H(); alert(typeof g);//object alert(g.name);//1 function H(){ this.name = 1; this.sc = 4; return this; } var g =new H(); alert(typeof g);//object alert(g.name);//1 alert(g.sc);//4 function L(){ this.name = 1; this.sc = 4; return new Array(1,2,3); } var l =new L(); alert(typeof l);//object alert(l);//1,2,3 alert(l.name);//undefined alert(l.sc);//undefined function E(){ this.name = 1; this.sc = 4; return 'ssss';//return的是引用类型new函数就返回return的引用类型对象,基本类型就还是返回函数类的实例对象。 } var e =new E(); alert(typeof e);//object alert(e);//object alert(e.charAt(1));//e.charAt is not a function alert(e.name);//1 alert(e.sc);//4 </script> </head> <body> </body> </html>
function F(){ this.n = 1; this.sc = 4; return function FF(){ this.n = 11; this.sc = 44; return function FFF(){ this.n = 111; this.sc = 442; } } } var f = new F(); alert(typeof f); alert(f.name);//FF, new F(),函数F值执行他的那层执行一遍 alert(f.n)//undefined function F(a){ this.n = 1; this.sc = 4; return new init(a); //new init(),如果init类没有return引用就返回init类的对象,否则返回init类中return的引用对象 } function init(){ this.n = 11; return {n:arguments[0]} } var d = F(3); alert(d.n);//3 function F(a){ this.n = 1; this.sc = 4; return new init(a); } function init(){ this.n = 11; return 44 } var d = F(3); alert(d.n);//11
new F() 不一定返回F类的对象,有可能返回F()函数里面返回的对象。
function F(a){ this.n = 1; this.sc = 4; return new G(a); //不一定返回G对象,返回{} } function G(){ this.n = 11; return {n:arguments[0]} } var d =new F(3); //不一定返回F的对象,返回的是F函数里面的new G()这个G的对象 alert(d.n);//3
本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/6898711.html,如需转载请自行联系原作者