<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
/*匿名函式的用法:*/
// 1. 字面量形式
let fn=function(){
console.log("匿名函式賦值給變數,成為一個有名字的函式");
}
fn();//這樣就是有名字的函式了
// 2. 物件函式形式
let obj={
name:"小明",
say:function(){
console.log(this.name+"說:hello");
}
}
obj.say(); // 小明說:hello
// 3. 作為事件處理函式
document.onclick=function(){
console.log("我被點選了");
}
// 4. 作為回撥函式
function add(a,b,callback){
let result=a+b;
callback(result);
}
add(1,2,function(result){
console.log("結果是:"+result);
})
function fn1(result){
setTimeout(() => {
console.log("我是主函式,被呼叫了");
result();
}, 2000);
}
fn1(function(){
console.log("我是回撥函式,被呼叫了");
})
//立即執行函式
(function(){
console.log("我是立即執行函式");
}());
//箭頭函式
let add1=(a,b)=>a+b;
console.log(add1(1,2)); // 3
</script>
</body>
</html>
在Js中匿名函式的幾種寫法
相關文章
- js中的匿名函式JS函式
- TypeScript 定義函式的幾種寫法TypeScript函式
- JS中建立函式的幾種方式JS函式
- TypeScript中函式過載寫法,你在第幾層!TypeScript函式
- js函式使用的幾種方式JS函式
- js確保正確this的幾種寫法JS
- 在JavaScript中建立名稱空間的幾種寫法JavaScript
- js中匿名函式的使用簡單介紹JS函式
- AnguarJS中鏈式的一種更合理寫法JS
- Js匿名函式小指南JS函式
- js 匿名自執行函式JS函式
- lambda匿名函式使用中的坑函式
- Python中的匿名函式-lambdaPython函式
- hash函式的幾種函式
- 單例的幾種寫法單例
- 不使用 JS 匿名函式的三個理由JS函式
- 面試中單例模式有幾種寫法?面試單例模式
- JavaScript 中匿名函式的遞迴呼叫JavaScript函式遞迴
- 深入理解 函式、匿名函式、自執行匿名函式函式
- js命名函式與匿名函式執行速度比較JS函式
- js匿名函式和具名函式執行效率比較JS函式
- 手寫CommonJS 中的 require函式JSUI函式
- 在JS中typeof返回的結果有哪幾種?JS
- 閉包函式(匿名函式)的理解函式
- 胡扯JS系列-匿名函式的自動執行JS函式
- 幾種函式呼叫方式函式
- PHP匿名函式PHP函式
- Ruby 匿名函式函式
- Golang匿名函式Golang函式
- JavaScript 匿名函式JavaScript函式
- Go 匿名函式Go函式
- JavaScript匿名函式JavaScript函式
- js中在一個函式中引用另一個函式中的函式,可以這麼做JS函式
- 在 JS 中實現 Laravel 的 ROUTE 函式JSLaravel函式
- 遞迴函式、演算法之二分法、三元表示式、各種生成式、匿名函式遞迴函式演算法
- python中函式引數傳遞的幾種方法Python函式
- java中寫建構函式的另外一種方式Java函式
- 學會TypeScript中函式過載寫法TypeScript函式