自己对js认识还是不够...
http://www.gracecode.com/posts/1009.html
http://blog.sanjh.cn/new-function-yao-zhu-yi-wen-ti.html
文章中的一句话一直不明白:
说明:
只要
new 表达式之后的 constructor
返回(return)一个引用对象(数组,对象,函数等),都将覆盖new创建的匿名对象。
如果返回(return)一个原始类型(无 return 时其实为
return 原始类型 undefined),那么就返回 new 创建的匿名对象。
后来燃烧脑细胞之后,得出的结论如下:
1
2
3
4 |
var
hello = new
function () { return
"hello, world!" }; alert(hello); |
代码执行应该是这样的:
1
2 |
var
hello = new
( function () { return
( "hello, world!" )};) alert(hello); |
对上面说明的理解应该是:
如果返回(return)一个原始类型(无
return 时其实为 return 原始类型 undefined),那么就返回 new
创建的匿名对象。
构造器constructor中,返回的是一个原始类型(即"hello,
world!"),则new表达式创建一个匿名对象,等同于var hello=new function(){/* something
*/}
1
2
3
4 |
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创建的匿名对象。
析后的代码变成:
1
2 |
var
hello = new
String( "abc" ); alert(hello); |
new function()随笔,布布扣,bubuko.com
原文:http://www.cnblogs.com/luoweihua7/p/3580447.html