前言
前端小白,哎其实连小白的水平都算不上,因为 magento 前端写的最多的还是php,css。今年准备突破一下我的JS水平,所以参加了若川的参加这个源码共读活动。以下是我的笔记,为了方便自己检索,我会大量用英文,尽量补齐中文翻译,原谅我的翻译水平有限,有些也不是很懂。
-
Tips:
-
open github in vscode mode in browser:
is to add ‘1s’ after ‘github’
<https://github.com/axios/axios> --> <https://github1s.com/axios/axios> -
断点调试
-
- 需要学习的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
//----------------------------------------------------------------------
-
Something new to me:
- Buffer
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()
Question:
why not use ? Buffer.isBuffer(buff);