本身对js认识仍是不够...
http://www.gracecode.com/posts/1009.html
http://blog.sanjh.cn/new-function-yao-zhu-yi-wen-ti.htmljavascript
文章中的一句话一直不明白:
说明:
只要 new 表达式以后的 constructor 返回(return)一个引用对象(数组,对象,函数等),都将覆盖new建立的匿名对象。
若是返回(return)一个原始类型(无 return 时其实为 return 原始类型 undefined),那么就返回 new 建立的匿名对象。html
后来燃烧脑细胞以后,得出的结论以下:java
var hello = new function() { return "hello, world!" }; alert(hello);
代码执行应该是这样的:数组
var hello = new (function() { return ("hello, world!")};) alert(hello);
对上面说明的理解应该是:闭包
若是返回(return)一个原始类型(无 return 时其实为 return 原始类型 undefined),那么就返回 new 建立的匿名对象。
构造器constructor中,返回的是一个原始类型(即"hello, world!"),则new表达式建立一个匿名对象,等同于var hello=new function(){/* something */}函数
var hello = new function() { return new String("abc"); }; alert(hello);
而代码返回的是new String('abc')时,则返回了一个对象,那么new String('abc')将会覆盖整个new function(){/* */},即等同于var hello=new String("abc");
只要 new 表达式以后的 constructor 返回(return)一个引用对象(数组,对象,函数等),都将覆盖new建立的匿名对象。post
析后的代码变成:spa
var hello = new String("abc"); alert(hello);
2014/05/06 PS: 若是返回的是一个引用类型,那么理解成闭包.就OK了code