JavaScript 函數(shù)
JavaScript?函數(shù)
函數(shù)基本概念
為完成某一功能的程式指令(語句)的集合,稱為函數(shù)。
JavaScript函數(shù)的分類
1、自訂函數(shù)(我們自己寫的函數(shù)),如:function funName(){}
2、系統(tǒng)函數(shù)(JavaScript自帶的函數(shù)),如alert函數(shù)。
函數(shù)的呼叫方式
1、普通呼叫:functionName(實(shí)際參數(shù)...)
2、通過指向函數(shù)的變量去調(diào)用:
var myVar=函數(shù)名;
myVar(實(shí)際參數(shù)...);
函數(shù)返回值
1.當(dāng)函數(shù)無明確傳回值時(shí),傳回的值就是"undefined"。
2.當(dāng)函數(shù)有回傳值時(shí),傳回值是什麼就回傳什麼。
var str="window.alert('好好學(xué)習(xí)');"; eval(str);//eval() 函數(shù)可計(jì)算某個字符串,并執(zhí)行其中的的 JavaScript 代碼。 /*自定義函數(shù)*/ function test(str){ alert(str); } window.alert(test);//輸出test函數(shù)的定義 //函數(shù)的調(diào)用方式1 test("好好學(xué)習(xí)"); //函數(shù)的調(diào)用方式2 var myFunction=test; myFunction("天天向上"); window.alert(myFunction); /*當(dāng)函數(shù)無明確返回值時(shí),返回的也是值 "undefined"*/ var retVal=test("test");//test函數(shù)執(zhí)行完之后,并沒有返回值,因此retVal變量接收到的返回值結(jié)果是undefined alert("retVal="+retVal);//輸出undefined