1、Promise.all
Promise.myAll = function (promises) {
return new Promise((resolve, reject) => {
if (typeof promises[Symbol.iterator] !== 'function') {
reject('TypeError: promises is not iterable')
}
if (promises.length === 0) {
resolve([])
} else {
const res = []
const len = promises.length
let count = 0
for (let i = 0; i < len; i++) {
Promise.resolve(promises[i])
.then((data) => {
res[i] = data
count += 1
if (count === len) {
resolve(res)
}
})
.catch((err) => {
reject(err)
})
}
}
})
}
function p1() {
return new Promise((resolve, reject) => {
setTimeout(resolve, 1000, 1)
})
}
function p2() {
return new Promise((resolve, reject) => {
setTimeout(resolve, 1000, 2)
})
}
Promise.myAll([p1(), p2()]).then(res => {
console.log(res)
})
2、Promise.race
Promise.myRace = function (promises) {
return new Promise((resolve, reject) => {
if (typeof promises[Symbol.iterator] !== 'function') {
reject('TypeError: promises is not iterable')
}
for (const item of promises) {
Promise.resolve(item).then(resolve, reject)
}
})
}
function p1() {
return new Promise((resolve, reject) => {
setTimeout(resolve, 1000, 1)
})
}
function p2() {
return new Promise((resolve, reject) => {
setTimeout(resolve, 1000, 2)
})
}
Promise.myRace([p1(), p2()]).then((res) => {
console.log(res)
})
3、Promise.any
Promise.myAny = function (promises) {
return new Promise((resolve, reject) => {
if (typeof promises[Symbol.iterator] !== 'function') {
reject('TypeError: promises is not iterable')
}
const len = promises.length
let count = 0
for (let i = 0; i < len; i++) {
Promise.resolve(promises[i]).then(resolve, (err) => {
count += 1
if (count === promises.length) {
reject(new Error('所有 promise 都失败'))
}
})
}
})
}
function p1() {
return new Promise((resolve, reject) => {
setTimeout(reject, 1000, 1)
})
}
function p2() {
return new Promise((resolve, reject) => {
setTimeout(resolve, 1000, 2)
})
}
Promise.myAny([p1(), p2()]).then((res) => {
console.log(res)
})
4、冒泡排序
function bubbleSort(arr) {
let len = arr.length
for (let i = 0; i < len - 1; i++) {
for (let j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]
}
}
}
return arr
}
const arr = [3, 1, 2, 5, 4]
console.log(bubbleSort(arr))
5、选择排序
function selectSort(arr) {
let len = arr.length
for (let i = 0; i < len - 1; i++) {
let minIndex = i
for (let j = i + 1; j < len; j++) {
if (arr[minIndex] > arr[j]) {
minIndex = j
}
}
[arr[i], arr[minIndex]] = [arr[minIndex], arr[i]]
}
return arr
}
const arr = [3, 1, 2, 5, 4]
console.log(bubbleSort(arr))
6、快速排序
function quickSort(arr) {
if (arr.length <= 1) return arr
const pivot = arr.shift()
const left = []
const right = []
for (let i = 0; i < arr.length; i++) {
if (arr[i] < pivot) {
left.push(arr[i])
} else {
right.push(arr[i])
}
}
return quickSort(left).concat([pivot], quickSort(right))
}
const arr = [3, 1, 2, 5, 4]
console.log(quickSort(arr))
7、call
Function.prototype.myCall = function (context = globalThis) {
const key = Symbol('key')
context[key] = this
const args = [...arguments].slice(1)
const res = context[key](...args)
delete context[key]
return res
}
const myName = { name: 'Jack' }
function say() {
const [age, height] = arguments
console.log(`My name is ${this.name}, My age is ${age}, My height is ${height}`)
}
say.myCall(myName, 16, 170)
8、apply
Function.prototype.myApply = function (context = globalThis) {
const key = Symbol('key')
context[key] = this
let res
if (arguments[1]) {
res = context[key](...arguments[1])
} else {
res = context[key]()
}
delete context[key]
return res
}
const myName = { name: 'Jack' }
function say() {
const [age, height] = arguments
console.log(`My name is ${this.name}, My age is ${age}, My height is ${height}`)
}
say.myApply(myName, [16, 170])
9、bind
Function.prototype.myBind = function (context = globalThis) {
const fn = this
const args = [...arguments].slice(1)
const newFunc = function () {
const newArgs = args.concat(...arguments)
if (this instanceof newFunc) {
fn.apply(this, newArgs)
} else {
fn.apply(context, newArgs)
}
}
newFunc.prototype = Object.create(fn.prototype)
return newFunc
}
const myName = { name: 'Jack' }
function say() {
const [age, height] = arguments
console.log(`My name is ${this.name}, My age is ${age}, My height is ${height}`)
}
const mySay = say.myBind(myName, 16, 170)
mySay()
10、instanceof
function myInstanceOf(obj, Fn) {
let proto = Object.getPrototypeOf(obj)
while (proto) {
if (proto === Fn.prototype) return true
proto = Object.getPrototypeOf(proto)
}
return false
}
const obj = { a: 1, b: 2 }
console.log(myInstanceOf(obj, Object))
11、new
function myNew(Fn, ...args) {
const obj = new Object()
obj.__proto__ = Fn.prototype
const res = Fn.apply(obj, args)
if (typeof res === 'object' && res !== null) {
return res
} else {
return obj
}
}
function Student(name, age) {
this.name = name
this.age = age
}
const stu = myNew(Student, 'Jack', 16)
console.log(stu)
12、统计页面中所有标签的种类和个数
function getTagCount() {
const tags = document.getElementsByTagName('*')
const tagNames = []
for (const val of tags) {
tagNames.push(val.tagName.toLocaleLowerCase())
}
const res = {}
for (const val of tagNames) {
if (!res[val]) {
res[val] = 1
} else {
res[val]++
}
}
return res
}
console.log(getTagCount())
13.类型判断
const myTypeOf = (data) => Object.prototype.toString.call(data).slice(8, -1).toLowerCase()
console.log(myTypeOf(1))
console.log(myTypeOf('1'))
console.log(myTypeOf(true))
console.log(myTypeOf([]))
console.log(myTypeOf({}))
console.log(myTypeOf(/^/))
console.log(myTypeOf(new Date()))
console.log(myTypeOf(Math))
console.log(myTypeOf(() => {}))