日期:2014-05-16 浏览次数:20358 次
在JavaScript里,定义函数的方式可以说也是花样百出,方式有很多,下面把几个常见到的定义函数方式汇总一下:
1.最常规定义方法 function name(){}
<script language="javascript" type="text/javascript">
function show(){alert("ok")};
show();
</script>
2.变量式定义 var n=function(){}
<script language="javascript" type="text/javascript">
var a=function(){alert("haha")};
a();
</script>
3.数组式定义 var n={f1:function(){},f2:function(){},f3:{f3_1:function,f3_2:function()}}
<script language="javascript" type="text/javascript">
var b={show:function(){alert("bbbb")},say:function(){alert("ccccc")},move:function(){alert("dddd")},run:{fast:function(){alert("run fast!")},slow:function(){alert("run slow!!!")}}};
b.run.fast();
</script>
4.面向对象式定义 var n=function(){n.m1=function(){};n.m2=function(){};}
<script language="javascript" type="text/javascript">
var c=function(){
this.method=function(){alert("c.method")};
this.method2=function(){alert("c.method2")};
}
var c2=new c();
c2.method2();
</script>