axios 工具函数

158 阅读2分钟

前言

前端小白,哎其实连小白的水平都算不上,因为 magento 前端写的最多的还是php,css。今年准备突破一下我的JS水平,所以参加了若川的参加这个源码共读活动。以下是我的笔记,为了方便自己检索,我会大量用英文,尽量补齐中文翻译,原谅我的翻译水平有限,有些也不是很懂。

  1. Tips:

    1. open github in vscode mode in browser:

      is to add ‘1s’ after ‘github’

      <https://github.com/axios/axios>  -->
      <https://github1s.com/axios/axios>
      
    2. 断点调试

Screenshot 2022-01-11 at 22.29.54.png

  1. 需要学习的tool functions:
// check array or not
function isArray(val) {
  return Array.isArray(val);
}
// check data is undefined or  not
function isUndefined(val) {
  return typeof val === 'undefined';
}

// determine if it is an object
function isObject(val) {
  return val !== null && typeof val === 'object';
}

//
// typeof is good to detect string, number, undefined, and "object"
//-----------------------------------------------------------------

// check whether it is Buffer type or not 
function isBuffer(val) {
  return val !== null && !isUndefined(val) && val.constructor !== null && !isUndefined(val.constructor)
    && typeof val.constructor.isBuffer === 'function' && val.constructor.isBuffer(val);
}
// check whether it is FormData 
function isFormData(val) {
  return Object.prototype.toString.call(val) === '[object FormData]';
}

// Object.prototype.toString.call
// is good to detect 
// FormData, Date, File, Blob, Function, URLSearchParams object, RegExp
//----------------------------------------------------------------------
  1. Something new to me:

    1. Buffer
💡 The `Buffer` class in Node.js is designed to handle raw binary data. Each buffer corresponds to some raw memory allocated outside V8. Buffers act somewhat like arrays of integers, but aren't resizable and have a whole bunch of methods specifically for binary data. The integers in a buffer each represent a byte and so are limited to values from 0 to 255 inclusive. When using `console.log()` to print the `Buffer` instance, you'll get a chain of values in hexadecimal values. [](https://nodejs.org/en/knowledge/advanced/buffers/how-to-use-buffers/) 💡 JavaScript 语言自身只有字符串数据类型,没有二进制数据类型。 但在处理像TCP流或文件流时,必须使用到二进制数据。因此在 Node.js中,定义了一个 Buffer 类,该类用来创建一个专门存放二进制数据的缓存区。 [](https://www.runoob.com/nodejs/nodejs-buffer.html)

b. Object.prototype.toString.call()

💡 这应该是早期常用来检测array的方法,当Array.isArray()出现之后,就被Array.isArray()替代了

This method will work for any primitive type and for any object. It always returns the name of the constructor for the variable. Put another way, Object.prototype.toString.call() is sort-of like instanceof in reverse, though it works fine inside of iframes.

console.log(Object.prototype.toString.call({})) // "[object Object]"
console.log(Object.prototype.toString.call([])) // "[object Array]"
console.log(Object.prototype.toString.call(null)) // "[object Null]"
const myDate = new Date()
console.log(Object.prototype.toString.call(myDate)) // "[object Date]"
const myRegExp = /howdy/
console.log(Object.prototype.toString.call(myRegExp)) // [object RegExp]"

c. typeof() vs instanceof vs Array.isArray()

The difference between Object.prototype.toString.call(), instanceof and Array.isArray() in judging array methods

Question:

why not use ? Buffer.isBuffer(buff);