new function(){},Function,new Function()区别
函数是JavaScript中很重要的一个语言元素,并且提供了一个function关键字和内置对象Function,下面是用法和它们之间区别。
使用方法一:
var foo01 = function() //or fun01 = function(){ var temp = 100; this.temp = 200; return temp + this.temp;}alert(typeof(foo01));alert(foo01()); var foo02 = new function(){ var temp = 100; this.temp = 200; return temp + this.temp;}alert(typeof(foo02));alert(foo02.constructor()); var obj = (new function(){ this.a = 123; this.fn = function(e){alert(e)}; //return new String('123'); //return 123;});obj.fn(123);可以理解返回一个对象。(new function(){ return '123';})与(new function(){ return new String('123');}) 又有什么区别呐?var foo3 = new Function('var temp = 100; this.temp = 200; return temp + this.temp;');alert(typeof(foo3));alert(foo3()); var foo4 = Function('var temp = 100; this.temp = 200; return temp + this.temp;');alert(typeof(foo4));alert(foo4());