Skip to content

[js] 第404天 axios为什么可以使用对象和函数两种方式调用?是如何实现的? #2416

Open
@haizhilin2013

Description

@haizhilin2013
Collaborator

第404天 axios为什么可以使用对象和函数两种方式调用?是如何实现的?

3+1官网

我也要出题

Activity

longhui520

longhui520 commented on May 24, 2020

@longhui520
  • axios.get 等于 axios({method:"get"})
  • 每个具体方式调用时,固定传入method值
ruochuan12

ruochuan12 commented on May 29, 2020

@ruochuan12

因为axios是函数。给函数上挂载了一些简写的方法,比如axios.postaxios.get等。
最后调用的都是Axios.prototype.request方法。
更具体的可以查看我的这篇文章。
@若川:学习 axios 源码整体架构,打造属于自己的请求库

hyj443

hyj443 commented on Oct 21, 2021

@hyj443

首先暴露的axios内部实现是指向一个wrap函数,然后这个函数上挂载了Axios.prototype上所有的属性和方法,包括get post delete等七种,所以axios就能直接这些方法,而且这些原型方法能实际调用是Axios.prototype.request。

axios(configObj)这样调用,就会执行这个wrap方法,它是对Axios.prototype.request封装。

所以无论怎么调用,最后都是靠Axios.prototype.request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jsJavaScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @haizhilin2013@ruochuan12@longhui520@hyj443

        Issue actions

          [js] 第404天 axios为什么可以使用对象和函数两种方式调用?是如何实现的? · Issue #2416 · haizlin/fe-interview