您的位置:首页 > Web前端 > JavaScript

通过JavaScript定义函数的注意点

2016-03-18 11:00 519 查看
在JavaScript中定义的函数主要有三种类型:

(1)一般函数

(2)动态函数

(3)匿名函数

一般函数的定义过程为:

function method(){
//alert("method run");
alert(arguments.length);
return 4;
}


在对一般函数进行定义时,需要注意:

1、要明确函数功能的未知内容,这是在明确参数列表。

2、明确函数功能的结果,这是明确return 的值。

3、每个函数都有返回值,当不写返回值时,默认的返回值是undefined。

4、定义完函数,对函数进行使用时,如进行 var s=method()的操作时,若误写成var s=method,在结果中将返回method对象实体中的内容,这是因为method对象存在内存中,进行 var s=method()的操作时,s在接收函数运算的结果;当执行var s=method时,s会指向method对象,当执行alert(s)时,s会将method对象的对象实体变成字符串打印出来。

动态函数的定义过程为:

var method =new Function("x","y","var sum=x+y; return sum;");
等价于:

founction method(x,y){
var sum=x+y;
teturn sum;
}
匿名函数:就是函数的一种简化形式。一般在事件处理上应用较多。定义过程为:

var show= function(){
alert("show run");
}

show();
等价于:

function method(){
alert("method run");
}
var show=mehod;
show();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: