JS篇 - 实现new、call、apply、bind、深拷贝、EventHub、promise、reduce、instanceof

实现 new

new的实现过程:
1.首先创建了一个新的空对象
2.设置原型,将对象的原型设置为函数的prototype(对象 继承 构造函数的原型链)
3.将构造函数的 this指向这个对象
4.根据构造函数的返回值类型,返回结果。如果是值类型,返回创建的对象;如果是引用类型,就返回这个引用类型的对象

function myNew(fn) {
   
	let obj = {
   };
	obj = Object.create(fn.prorotype)
	let args = Array.prototype.slice.call(arguments, 1) //获取除去fn之外的参数
	let result = fn.call(obj, ...args)
	return typeof result === 'object' ? result : obj;
	// 在构造函数中:
	// 如果return值类型,那么对构造函数没有影响,实例化对象返回空对象;
	// 如果return引用类型(数组、函数、对象),那么实例化对象就会返回该引用类型
	// 可测试下面 例1 中的代码,查看返回情况
}
function foo() {
   
	this.name = 'ciel';
	this.arg = arguments[0]
}
foo.prototype.callName = function() {
   
	console.log(this.name)
}
//测试
let test = myNew(foo,'hhh','123','sagag')
test.callName()
console.log(test.arg)1function foo() {
   
	   this.name = 'ciel'
	   return function() {
   
	
	   }
	 }
	 new foo() //  fn(){}
	 function bar() {
   
	   this.name = 'ciel'
	   return 1
	 }
	 new bar() // {name: ciel}
==================================
const temp = {
   }
temp.__proto__ = fn.prototype; //核心 用fn.prototype是否为对象原型来判断是否为new的情况
fn.apply(temp,[...args])
return temp

什么是this、call、apply、bind?

后三者作用都是动态改变了上下文,也就是改变了this的指向。
call、apply立即执行 , bind不立即执行.

  • Dad.call( this , height) //call方法接收的是若干个参数列表
  • Dad.apply( this , [height]) //apply方法接受的是一个包含多个的参数数组
  • Dad.bind( this )( height ) //bind方法是返回改变指向的函数

实现 call

myCall 实现过程:
1. myCall 应该挂在Function.prototype 上
2. fn 的 this 指向为 obj
3. myCall 的 args 都传给 fn

Function.prototype.myCall = function(target, ...args) {
   
	//this 指向调用 myCall 函数的对象
	iftypeof this !== "function") {
   
		throw new TypeError("not a function");
	}
	target = target || window
	target.fn = this // 隐式绑定,改变构造函数的调用者间接改变 this 指向
	let result = target.fn(...args);
	return result;
]
// 测试
let obj = {
   name:123}
function foo(...args) {
   
	console.log(this.name,args)
}
let s = foo.myCall(obj,'111','222');

实现 apply

实现思路:
- apply 与 call 的区别在:apply参数要为数组,其他和call实现一样
1. myCall 应该挂在Function.prototype 上
2. fn 的 this 指向为 obj
3. myCall 的 args 都传给 fn

Function.prototype.myApply = function(target) {
   
	if(typeof this !== "function") {
   
		throw new TypeError('not a fu
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
call、applybind都是用来改变函数中的this指向的方法。其中,call和apply可以直接调用函数并传递参数,而bind则返回一个新的函数,需要手动调用。 具体实现方案如下: - call的实现: 1. 给想要绑定的对象设置一个属性,并将该属性指向需要调用的函数。 2. 使用该对象调用函数,并传递参数。 3. 结束调用后,删除该属性。 - apply实现: 1. 给想要绑定的对象设置一个属性,并将该属性指向需要调用的函数。 2. 使用该对象调用函数,并传递参数数组。 3. 结束调用后,删除该属性。 - bind实现: 1. 创建一个新的函数,并将原函数作为其中的属性保存起来。 2. 当新函数被调用时,将之前绑定的对象作为this,并传递参数。 3. 返回新函数供后续调用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [如何实现call、applybind](https://blog.csdn.net/XIAO_A_fighting/article/details/116701887)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [原生JS实现 call apply bind](https://download.csdn.net/download/weixin_38628990/14046564)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值