JavaScript自定義函數用法詳解
更新時間:2022年03月10日 16:23:42 作者:.NET開發(fā)菜鳥
本文詳細講解了JavaScript自定義函數的用法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
JavaScript中的函數分為兩種:系統(tǒng)函數和自定義函數,這里主要講解自定義函數。
自定義函數
1、語法:

注意:
- 傳入的參數是可選的。
例如:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>自定義函數</title>
<script>
// 語法1自定義無參函數
function custom(){
document.write("自定義無參函數,使用第一種語法定義"+"<br />")
};
// 語法2
var customer=function(){
document.write("自定義無參函數,使用第二種語法定義"+"<br />")
};
// 定義有參函數
function customWithPara(i){
document.write("自定義有參函數,使用第一種語法定義,i的值是:"+i+"<br />")
};
// 語法2
var customerWithPara=function(i){
document.write("自定義有參函數,使用第二種語法定義,i的值是:"+i+"<br />")
};
</script>
</head>
<body>
</body>
</html>2、函數的調用
函數可以通過函數名加上括號中的參數進行調用。
例如:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>函數調用</title>
</head>
<body>
<script>
// 定義無參函數
function custom(){
document.write("這是無參的函數"+"<br />");
};
// 定義無參的函數變量
var customer=function(){
document.write("這是無參的函數變量"+"<br />");
};
// 定義有參函數
function customWithPara(para){
document.write("這是有參函數,參數值是:"+para+"<br />");
}
// 定義有參的函數變量
var customerWithPara =function(para){
document.write("這是有參的函數變量,參數值是:"+para+"<br />");
}
// 函數調用
// 1、調用無參函數
custom();
// 2、調用有參函數
customWithPara(45);
// 無參函數變量的調用
customer();
// 有參函數變量的調用
customerWithPara(23);
</script>
</body>
</html>結果:

注意:
- 調用函數時需要注意函數調用的順序。如果是自定義函數,那么也可以在函數定義之前調用函數,因為這時會自動把函數的定義放到最前面。如果是通過變量的形式定義函數,那么必須先定義函數才能調用。
看下面的例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>函數調用</title>
</head>
<body>
<script>
// 函數調用
// 1、調用無參函數
custom();
// 2、調用有參函數
customWithPara(45);
// 無參函數變量的調用
customer();
// 有參函數變量的調用
customerWithPara(23);
// 定義無參函數
function custom(){
document.write("這是無參的函數"+"<br />");
};
// 定義無參的函數變量
var customer=function(){
document.write("這是無參的函數變量"+"<br />");
};
// 定義有參函數
function customWithPara(para){
document.write("這是有參函數,參數值是:"+para+"<br />");
}
// 定義有參的函數變量
var customerWithPara =function(para){
document.write("這是有參的函數變量,參數值是:"+para+"<br />");
}
</script>
</body>
</html>結果:

3、匿名函數
匿名函數:顧名思義,即沒有函數名稱的函數。其語法如下圖所示:

例如:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>匿名函數</title>
</head>
<body>
<script>
// 傳統(tǒng)定義函數的方式
function fn(){
document.write("這是傳統(tǒng)函數的定義"+"<br />");
};
// 調用
fn();
// 匿名函數的定義和調用
(function(){
document.write("這是匿名函數"+"<br />");
})();
</script>
</body>
</html>結果:

4、匿名函數的應用
匿名函數可以作為函數的參數進行調用,看下面的例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>匿名函數的應用</title>
<script>
// 匿名函數應用
function fun(para){
document.write("參數的值是:"+para+"<br />");
};
// 用匿名函數作為函數的參數
fun(function(){
return 5;
}());
// 也可以使用下面的方式
function fu(para){
document.write("參數的值是:"+para()+"<br />");
};
fu(function(){
return "56";
});
</script>
</head>
<body>
</body>
</html>結果:

到此這篇關于JavaScript自定義函數用法的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Javascript中的getUTCHours()方法使用詳解
這篇文章主要介紹了Javascript中的getUTCHours()方法使用詳解,是JS入門學習中的基礎知識,需要的朋友可以參考下2015-06-06

