标签:
1、语法:
1 var add=function(a,b){ 2 return a+b; 3 }
第二部分是函数名,可省略第一部分是保留字function
1 var myObject={ 2 value:0, 3 increment:function(inc){ 4 this.value+=typeof inc===‘number‘?inc:1; 5 } 6 }; 7 8 myObject.increment(); 9 document.writeln(myObject.value);//1 10 11 myObject.increment(2); 12 document.writeln(myObject.value);//3
<script> var add=function(a,b){ if(typeof a!==‘number‘||typeof b!==‘number‘ ){ throw{ name:‘TypeError‘, message:‘add needs numbers‘ }; } return a+b; }; var try_it=function(){ try{ document.writeln(add("safa")); }catch(e){ document.writeln(e.name+‘:‘+e.message); } }; </script> <button onclick="try_it()">点击我 </button>
var sum=function(){ var i,sum=0; for(i=0;i<arguments.length;i+=1){ sum+=arguments[i]; } return sum; }; document.writeln(sum(1,2,3,4));
标签:
原文地址:http://www.cnblogs.com/gugibv/p/5144507.html