let proto = target.__proto__ while (true) { if (proto === null) returnfalse if (proto === Fn.prototype) returntrue proto = proto.__proto__ } } functionA() {} const a = new A() console.log(_instanceof(a, A)) // true console.log(_instanceof(1, A)) // false 复制代码
Array.prototype.map
map 中的 exc 接受三个参数,分别是: 元素值、元素下标和原数组
map 返回的是一个新的数组,地址不一样
// 这里不能直接使用箭头函数,否则无法访问到 this Array.prototype._map = function (exc) { const result = [] this.forEach((item, index, arr) => { result[index] = exc(item, index, arr) }) return result } const a = newArray(2).fill(2) console.log(a.map((item, index, arr) => item * index + 1)) // [1,3] console.log(a._map((item, index, arr) => item * index + 1))// [1,3] 复制代码
const once = (fn) => { let res, isFirst = true returnfunction (...args) { if (!isFirst) return res res = fn.call(this, ...args) isFirst = false return res } } 复制代码
// 有一个失败则返回失败的结果,全部成功返回全成功的数组 Promise.all = function (promiseList = []) { return new Promise((resolve, reject) => { const result = [] let count = 0 if (promiseList.length === 0) { resolve(result) return } for (let i = 0; i < promiseList.length; i++) { Promise.resolve(promiseList[i]).then(res => { result[i] = res count++ // 不能直接通过 result.length 进行比较,因为 会存在下标大的先赋值 // 例如 i = 3 第一个返回结果,此时数组变为[empty,empty,empty,res] if (count === promiseList.length) { resolve(result) } }).catch(e => { reject(e) }) } }) } // 返回第一个成功或失败的结果 Promise.race = function (promiseList = []) { return new Promise((resolve, reject) => { if (promiseList.length === 0) { return resolve([]) } for (let i = 0; i < promiseList.length; i++) { Promise.resolve(promiseList[i]).then(res => { resolve(res) }).catch(e => { reject(e) }) } }) } // 无论成功约否都返回,但是会添加一个 status 字段用于标记成功/失败 Promise.allSettled = function (promiseList = []) { return new Promise((resolve, reject) => { const result = [] let count = 0
const addRes = (i, data) => { result[i] = data count++ if (count === promiseList.length) { resolve(result) } }
if (promiseList.length === 0) return resolve(result) for (let i = 0; i < promiseList.length; i++) { Promise.resolve(promiseList[i]).then(res => { addRes(i, { status: 'fulfilled', data: res }) }).catch(e => { addRes(i, { status: 'rejected', data: e }) }) } }) } // AggregateError,当多个错误需要包装在一个错误中时,该对象表示一个错误。 // 和 Promise.all 相反,全部失败返回失败的结果数组,有一个成功则返回成功结果 Promise.any = function (promiseList = []) { return new Promise((resolve, reject) => { if (promiseList.length === 0) return resolve([]) let count = 0 const result = [] for (let i = 0; i < promiseList.length; i++) { Promise.resolve(promiseList[i]).then(res => { resolve(res) }).catch(e => { count++ result[i] = e if (count === promiseList.length) { reject(new AggregateError(result)) } }) } }) } 复制代码
整数千分位加逗号
1234567 -> 1,234,567 复制代码
function toThousands(num) { num = num.toString() let result = '' while (num.length > 3) { result = ',' + num.substring(num.length - 3) + result num = num.substring(0, num.length - 3) } result = num + result return result } console.log(toThousands(1234567)) // 1,234,567 console.log(toThousands(123456)) // 123,456 复制代码