小程序笔记—回调函数(typeof cb == "function" && cb(obj)),success、fail、complete方法是怎么写出来的

先看使用回调函数的代码:

  this.testCallmethod({
      type:0,
      success:function(res){
        console.log(res)
      },
      fail:function(e){
        console.log(e)
      },
      complete: function(res) {
        console.log(res)
      }
    })

再看回调函数的代码:

testCallmethod(cb) {
	//可执行一些异步操作,将下方逻辑放在异步中
    if (cb.type == 0) {
      typeof cb.success == "function" && cb.success("success")
    }else{
      typeof cb.fail == "function" && cb.fail("fail")
    }
    typeof cb.complete == "function" && cb.complete("complete")
  },

运行结果
在这里插入图片描述)
在这里插入图片描述

发布了62 篇原创文章 · 获赞 48 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/WeiHan_Seven/article/details/104291055