手动实现一些函数的封装

正文
  • 全局对象
    • node 中的 全局 this指向的是module.exports,而不是全局对象global
      • 在外部函数中的this指向global,匿名函数也是
    • 浏览器环境下全局this就是window
      • 在外部函数中的this指向window,匿名函数也是
  • reverse
    - 不改变原数组版,如果想要改变,那么不要deepClone即可
Array.prototype.myReverse = function () {
    const length = this.length
    const arr = deepClone(this)
    for (let i = 0; i < Math.floor(length / 2); i++) {
        let temp = arr[i]
        arr[i] = arr[length - 1 - i]
        arr[length - 1 - i] = temp
    }
    return arr
}

const arr = ['a', 'b', 'c', 'd']

console.log(arr.myReverse());
console.log(arr)

function deepClone(target) {
    if (typeof target !== 'object') {
        return target
    }

    let result
    if ({}.toString.call(target) === '[object Object]') {
        result = {}
    } else {
        result = []
    }

    for (let key in target) {
        if (target.hasOwnProperty(key)) {
            result[key] = deepClone(target[key])
        }
    }

    return result
}

  • forEach
    • 兼容node环境或者浏览器环境
Array.prototype.myForEach = function (callback, thisArg = typeof global === 'undefined' ? window : global) {
   for (let i = 0; i < this.length; i++) {
       callback.call(thisArg, this[i], i, this)
   }
}
  • filter
    • 兼容node环境或者浏览器环境
    • 改变原数组
Array.prototype.myFilter = function (callback, thisArg = typeof global === 'undefined' ? window : global) {
        for (let i = 0; i < this.length; i++) {
            if (!callback.call(thisArg, this[i], i, this)) {
                this.splice(i, 1)
                i--
            }
        }
    }
  • map
    • 兼容node环境或者浏览器环境
    • 改变原数组
Array.prototype.myMap = function (callback, thisArg = typeof global === 'undefined' ? window : global) {
        for (let i = 0; i < this.length; i++) {
            this[i] = callback.call(thisArg, this[i], i, this)
        }
    }
  • every
    • some同理,省略
    • 兼容node环境或者浏览器环境
Array.prototype.myEvery = function (callback, thisArg = typeof global === 'undefined' ? window : global) {
   let flag = true

   for (let i = 0; i < this.length; i++) {
       if (!callback.call(thisArg, this[i], i, this)) {
           flag = false
           break
       }
   }

   return flag
}
  • find
    • findIndex同理,省略
    • 兼容node环境或者浏览器环境
Array.prototype.myFind = function (callback, thisArg = typeof window === "undefined" ? global : window) {
    for (let i = 0; i < this.length; i++) {
        if (callback.call(thisArg, this[i], i, this)) {
            return this[i]
        }
    }
}
  • 字符串是否是回文序列(不区分大小写)
String.prototype.isPalindrome = function () {
    for (let i = 0; i < Math.floor(this.length / 2); i++) {
        if (this[i].toLowerCase() !== this[this.length - 1 - i].toLowerCase()) {
            return false
        }
    }
    return true
}

String.prototype.isPalindrome = function () {
    return this.toLowerCase() === this.toLowerCase().split('').reverse().join('')
}
结语

如果对你有帮助的话,请点一个赞吧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值