JavaScript常见api手写实现(持续更新)

1.手写call方法

Function.prototype.mycall = function (obj)  {
	let obj = obj || window
	let args = Array.from(arguments)
	obj.fn = this
	let res = obj.fn(...args)
	delete obj.fn
	return res
}

2.手写bind方法

Function.prototype.mybind = function ()  {
	let args = Array.from(arguments)
	let thisArg = args.shift()
	let fn = this
	let fnBound = function () {
		let newArgs = args.concat(Array.form(arguments))
		// 判断是否为构造函数
		thisArg = this instanceOf fnBound ? this : thisArg
		return fn.call( thisArg, ...newArgs )
	}
	 let fnNo = new Function()
	 fnNo.prototype = fn.prototype
	 fnBound .prototype = new fnNo()
    	return fnBound 
}

3.数组扁平化

function flatter(arry) {
    let res = []
    for(let i =0; i < arry.length; i++) {
        if (Array.isArray(arry[i])) {
            res.concat(flatter(arry[i]))
        } else {
            res.push(arry[i])
        }
    }
    return res
}

4.函数柯里化

function add () {
    let args = Array.prototype.slice.call(arguments)
    let _adder = function() {
        args.push(...arguments)
        return _adder
    }
    _adder.toString = function () {
        return args.reduce((a, b) => {
            return a+ b
        })
    }
    return _adder
}

5.new 函数

Function.prototype.new = function(obj) {
    let res = Object.create(obj)
    let args = Array.from(arguments).slice(1)
    let ret = obj.call(res, ...args)
    return ret || res
}

5.object.create 函数

Object.prototype.create =function (proto, propertyObject = undefined) {
    // 创建一个空的构造函数 F
    function F() { }
    // F 原型指向 proto
    F.prototype = proto
    // 创建 F 的实例
    const obj = new F()
    // propertiesObject有值则调用 Object.defineProperties
    if (propertyObject != undefined) {
        Object.defineProperties(obj, propertyObject)
    }
    if (proto === null) {
        // 创建一个没有原型对象的对象,Object.create(null)
        obj.__proto__ = null
    }
    // 返回 这个 obj
    return obj
}

6.object.assgin 函数

Object.prototype.assgin = function (target, ...source) {
    let res = Object(target);
    source.forEach(function (obj) {
        if (obj != null) {
            for (let key in obj) {
                if (obj.hasOwnProperty(key)) {
                    res[key] = obj[key];
                }
            }
        }
    })
    return res;
}

7.手写promise.all()

Promise.prototype.all = (arr) => {
    let result = new Array(arr.length);
    let counter = 0;//注意这里通过一个变量取保存它的成功的数量
    return new Promise((resolve, reject) => {
        arr.forEach((item, index) => {
            let i = index;
            Promice.resolve(item).then(value => {
                result[i] = value;
                counter ++;
                if (counter === arr.length) {//通过conter变量比较,而不是直接通过result.length去判断
                    resolve(result);
                }
            }).catch(err => {
                reject(err)
            })
        })
    });
}

8.手写promise.race()

Promise.prototype.race = (arr) => {
    return new Promise((resolve, reject) => {
        arr.forEach((item, index) => {
            Promice.resolve(item).then(value => {
                 resolve(value);
            }, err => {
                reject(err)
            })
        })
    });
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值