function,new function,Function,new Function 之间的区别

测试一:

var fud01 = function() 
{
var temp = 100;
this.temp = 200;
return temp + this.temp;
}

alert(typeof(fud01));
alert(fud01());

运行结果:
function 300

最普通的function使用方式,定一个JavaScript函数。在大扩号内的变量作用域中,this指代fud01的所有者。

测试二: 

var fud02 = new function()
{
var temp = 100;
this.temp = 200;
return temp + this.temp;
}

alert(typeof(fud02));
alert(fud02.constructor());

运行结果: object  300

实际上这是定一个JavaScript中的用户自定义对象,不过这里是个匿名类。这个用法和函数本身的使用基本没有任何关系,在大扩号中会构建一个变量作用域,this指代这个作用域本身。

测试三:

var fud3 = new Function('var temp = 100; this.temp = 200; return temp + this.temp;');

alert(typeof(fud3));
alert(fud3());

运行结果: function 300

使用系统内置函数对象来构建一个函数,这和方法一中的第一种方式在效果和初始化优先级上都完全相同,就是函数体以字符串形式给出。

测试四:

var fud4 = Function('var temp = 100; this.temp = 200; return temp + this.temp;');

alert(typeof(fud4));
alert(fud4());

运行结果: function 300

这个方式是不常使用的,效果和方法三一样,不过不清楚不用new来生成有没有什么副作用。

详解new function(){}和function(){}()

原文地址:https://www.cnblogs.com/net-saiya/p/6006074.html