首页 > 编程语言 >挑战前端基础120题--JavaScript 中如何实现链式调用的函数?

挑战前端基础120题--JavaScript 中如何实现链式调用的函数?

时间:2024-04-23 23:12:19浏览次数:18  
标签:resolve console log -- res JavaScript 120 调用 return

一. 何为链式调用?

链式调用是一种简化过程的编码方式,使代码看起来更加简洁~它允许你通过在方法调用之间返回对象本身,从而连续地调用多个方法;比较常见的链式调用:jQuery, Promise等,通过多次书写.()操作来调用。

二. 实现的原理?

每次执行完成后返回自己/新的自己,这样可以确保后续的方法仍然在当前环境下执行;

三. 如何实现?

let p = {
  then() {
     console.log('输出-->then');
     return this;
  }
}
console.log(p.then().then());

 

function myPromise () {
    return this;
}
myPromise.prototype.then = function () {
    console.log('my promise');
    return new myPromise(); // this
}

let myP1 = new myPromise();
console.log(myP1.then().then().then())

 四. 深入理解下Promise链式调用的规则?

let pro = new Promise((resolve, reject) => {
    resolve('我是一个promise对象')
})
console.log(pro); // Promise
pro.then(res => {
    console.log(res); // 我是一个promise对象
}).then(res => {
    console.log(res);  // undefined
    return 123;
}).then(res => {
    console.log(res); // 123
    return new Promise(resolve => {
        resolve('456')
    })
}).then(res => {
    console.log(res); // 456
}).then(res => {
    console.log(res); //undefined
}).then(res => {
    console.log(res); // undefined
    return '我是直接返回的结果';
}).then()
    .then('我是字符串').
    then(function (res) {
    console.log(res); // 我是直接返回的结果
})

总结:

1. 只要有then() 并且触发了resolve,链式就会执行到结尾,res 就是为resolve参数传入的值;

2. 每个函数也可以使用return返回值,没有的话后续下一个then中回调函数的值默认undefined;

3. 返回值如果是一个普通值,则直接传递到下个then,如果是一个Promise对象,那么此Promise对象的resolve传入的值就会被当作后续下一个then中回调函数的值;

4. 如果then中传入的值不是回调函数或者没有传递值,链式调用并不是中断,而是继续往下执行,这是后面获取到的res为最后一次return/resolve传入的值;

标签:resolve,console,log,--,res,JavaScript,120,调用,return
From: https://www.cnblogs.com/sulishibaobei/p/18153852

相关文章

  • Markdowmtoblog 文件图片上传
    pycnblog博客园上传markdown文件https://www.cnblogs.com/df888/p/11826480.html注意博客园6.21更新,MetaWeblog现在不支持密码登录,需要通过访问令牌(accesstoken)登录,在博客后台设置页面,允许MetaWeblog博客客户端访问,下方有MetaWeblog访问令牌,点击查看,创建访问令牌。功能一......
  • 6-1 使用函数求特殊a串数列和
    给定两个均不超过9的正整数a和n,要求编写函数fn(a,n)求a+aa+aaa++⋯+aa⋯aa(n个a)之和,fn须返回的是数列和函数接口定义: fn(a,n)其中a和n都是用户传入的参数。a的值在[1,9]范围;n是[1,9]区间内的个位数。函数须返回级数和裁判测试程序样例: /*请在这里填写答......
  • 6-2 使用函数求余弦函数的近似值
    本题要求实现一个函数,用下列公式求cos(x)近似值,精确到最后一项的绝对值小于eps(绝对值小于eps的项不要加):cos(x)=0!x0​−2!x2​+4!x4​−6!x6​+...函数接口定义:funcos(eps,x),其中用户传入的参数为eps和x;函数funcos应返回用给定公式计算出来,保留小数4位。函数接口定义: 函......
  • 151. 反转字符串中的单词
    题目链接:151.反转字符串中的单词这题主要是熟悉java一些库的调用,先放代码:classSolution{publicStringreverseWords(Strings){s=s.trim();//去除两边多余空格List<String>list=Arrays.asList(s.split("\\s+"));//将字符串按空格切割Coll......
  • QT信号槽机制
    QT信号槽机制例如将按钮点击信号连接到文本框的更新槽函数上点击时按钮发出点击信号文本框接收信号执行更新操作下面通过一个简单的计算球体体积的例子来介绍信号槽机制:dialog.h#ifndefDIALOG_H#defineDIALOG_H#include<QDialog>#include<qlabel.h>#include<qpus......
  • 处理DataFrame的技巧
    DataFrame:user_idnameagecountryscorecontinent1001Mark55Italy4.5Europe1000John33USA6.7America1002Tim41USA3.9America1003Jenny12Germany9Europe如果在构造DataFrame时没有提供列名,那么pandas会用从0开始的数字为列编号。us......
  • 【VMware by Broadcom】产品版本和内部版本号
    VMwareESXi/ESXBuildnumbersandversionsofVMwareESXi/ESX(2143832)VMwarevCenterServerBuildnumbersandversionsofVMwarevCenterServer(2143838)VMwarevCenterChargebackBuildnumbersandversionsofVMwarevCenterChargeback (2143841)V......
  • heackmyvmRegistry
    heackmyvmRegistryprogrambamuwe@bamuwe:~$checksecprogram[*]'/program'Arch:amd64-64-littleRELRO:PartialRELROStack:NocanaryfoundNX:NXunknown-GNU_STACKmissingPIE:NoPIE(0x400000)S......
  • [BJDCTF 2020]YDSneedGirlfriend
    [BJDCTF2020]YDSneedGirlfriendUAF|所谓UAF漏洞是指程序在运行时通过悬空指针(悬空指针是指仍然指向已被释放内存空间的指针)访问已经被释放的内存.bamuwe@bamuwe:~/YDSneedGirlfriend$lddgirlfriendlinux-vdso.so.1(0x00007ffd09fec000)/home/bamuwe/pw......
  • 14. 最长公共前缀
    题目链接:实现一、\(\rmTrie\)求多串的最长公共前缀,首先想到\(\rmTrie\)。classSolution{public:staticconstintN=210;intch[N][26],idx,cnt[N];voidinsert(strings){intp=0;for(inti=0;i<s.size();i++){......