js手写实现各种函数

  1. Object.create()
Object.prototype._create = function(obj) {
	 if(Object.prototype.toString.call(obj) !== '[object Object]') return
	 let newObj = {}
	 newObj.__prop__ = obj
	 return newObj
}
  1. map
Array.prototype._map = function(fn) = {
	if(typeof fn !== 'function') return
	let newArr = []
	for(var i=0;i<this.length;i++) { // this此时为当前数组
		newArr.push(fn(this[i]))
	}
	return newArr 
}
  1. filter
Array.prototype._filter = function(fn) = {
	if(typeof fn !== 'function') return
	let newArr = []
	for(var i=0;i<this.length;i++) {
		if(fn(this[i])) {
			newArr.push(this[i])
		}
	}
	return newArr 
}
  1. reduce
Array.prototype._reduce = function(fn, init) = {
	if(typeof fn !== 'function') return
	let preVal = 0
	if(init) {
		preVal = init
	}
	let newArr = []
	for(var i=0;i<this.length;i++) {
		preVal = fn(preVal, this[i])
	}
	return preVal
}
  1. call
Function.prototype._call = function(target = window, ...args) {
	target.fn = this
	let result = target.fn(...args)
	delete target.fn
	return result
}
  1. bind
Function.prototype._bind = function(target,...args1) {
    let fn = this
    return function(...args2) {
        return fn.call(target, ...args1, ...args2)
    }
}
fn.getVal._bind(this, 1)(2, 3)
  1. Object.freeze() - 冻结对象(不可删除/改写/新增属性)
Object.prototype._objectFreeze = function(object) => {
    if(Object.prototype.toString.call(object) !== '[object Object]') return
    Object.keys(object).forEach(item => {
        Object.defineProperty(object, item, {
            configurable: false, // 不可删除的
            enumerable: true, // 可枚举的
            writable: false // 不可重写的
        })
    })
    Object.seal(object); // 不可添加新属性
}
  1. 浅拷贝
const _shallowClone = (target) => {
	if(typeof target === 'object && object !== null) {
	const constructor = target.constructor 
		if(/^(Function|RegExp|Date|Map|Set)$/i).test(constructor.name)) return target
		let cloneObj = Array.isArray(target) ? [] : {}
		for(prop in target) {
			cloneObj[prop] = target[prop]
		}
		return cloneObj
	}else {
		return target
	}
}
  1. 寄生组合式继承
function Human(name) {
    this.name = name
    this.kingdom = 'animal'
    this.color = ['yellow', 'white', 'brown', 'black']
}

Human.prototype.getName = function() {
    return this.name
}

function Chinese(name,age) {
    Human.call(this, name)
    this.color = 'yellow'
    this.age = age
}

Chinese.prototype = Object.create(Human.prototype)
Chinese.prototype.constructor = Chinese
Chinese.prototype.getAge = function() {
    return this.age
}
  1. 发布订阅模式
class EventEmitter {
    constructor() {
        this.events = {}
    }
    on(event, fn) {
       if(!this.events[event]) {
           this.events[event] = [fn]
       }else {
           this.events[event].push(fn)
       }
    }
    
    emit(event, ...args) {
        if(this.events[event]) {
            this.events[event].forEach(callback => callback(...args))
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值