JS里的function与newfunction?
document.body.onload=function(xxx)
document.body.onload=new function(xxx)
document.body.onload=function(){xxxxxxxxx}
首先第一种:
楼主不要写成function(xxx),否则大家都以为是function关键字,其实只是个自定义的函数。
至于下面这种写法效果是一样的,但是注意了这里有new关键字,而且不是function,而是Function,比如说:
document.onmouseup=new Function("flag=true");
第三种即最常见的一种,语法为:
function(){statement}
-----------------------------------
一.
var foo01 = function() {
var temp = 100;
this.temp = 200;
return temp + this.temp;
}
这里重载了foo01函数,效果和function foo01(){statement}差不多,但是区别在于var foo01=function(){statement}重装了foo01函数,也就相当于个模型,区别还是有的,比如说有个函数分别调用了这两种方式写的函数foo01,但是例子中的写法将foo01的方法继承了,但是function foo01(){}这种写法则直接执行了函数,所以两个写法都很有用处。
二.
var foo02 = new function()
{
var temp = 100;
this.temp = 200;
return temp + this.temp;
}
这种写法不常见,但是它和例一差不多,只不过多了关键字new,很明显该函数必须先定义自定义函数的模型,然后才能对此函数用new关键字来实例化。
三.
var foo3 = new Function(’var temp = 100; this.temp = 200; return temp + this.temp;’);
在上面已经提及过了。
Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州京杭网络科技有限公司 版权所有