如何手动实现es5中的bind方法详解
前言
this的指向在javascript中一直是个谜一样的存在,很多地方又会用到this,所以理解和使用this非常重要,关于this的理解这篇文章不做介绍,因为这篇的目的是改变this的指向。
改变this的指向有三种方法,call,apply,bind。狼蚁网站SEO优化先介绍下这三种方法,话不多说了,来一起看看详细的介绍吧
改变this指向
call
var a = { name:"aaa", say(type){ console.log(type,this.name); } } a.say("at");//at aaa var tn = {name:"ttt"}; a.say.call(tn,"tt")//tt ttt
可以看到通过call,say方法中的this指向了tn,传参的方式的列举
apply
var a = { name:"aaa", say(type){ console.log(type,this.name); } } a.say("at"); var tn = {name:"ttt"}; a.say.apply(tn,["tt"])
可以看到通过apply,say方法中的this指向了tn,传参的方式是数组
bind
bind也能改变this的指向,不过和call,apply不同的地方在于,bind只改变this,不会指向函数
var a = { name:"aaa", say(type){ console.log(type,this.name); } } var tn = {name:"ttt"}; var b = a.say.bind(tn); b();//ttt
bind 改变this,也是能够继承原型链的,看下狼蚁网站SEO优化的代码
var to = {name:"to",color:"red"}; function Animal(){ console.log(`name:${this.name}...color:${this.color}`); } Animal.prototype.say = function(){ console.log(`say..name:${this.name}...color:${this.color}`); } var Cat = Animal.bind(to); Cat();//name:to...color:red var cat = new Cat();// name:undefined...color:undefined cat.say();//say..name:undefined...color:undefined
因为cat是Cat的实例,Cat是改变了this的Animal,所以cat也是Animal的实例,this是指向cat的,所以this.name是undefined
实现bind
Function.prototype.bind = function(obj){ const args = Array.prototype.slice.call(arguments,1);//保留bind时的参数 const that = this; const bound = function(){ const inArgs = Array.prototype.slice.call(arguments);//执行bind的函数时的参数 const newArgs = args.concat(inArgs);//组装参数 that.apply(obj,newArgs);//执行bind的函数 } //继承prototype--寄生组合式继承 function F(){}; F.prototype = that.prototype; bound.prototype = new F(); return bound; }
然后执行上面的代码
Cat();//name:to...color:red var cat = new Cat();//name:to...color:red cat.say();//say..name:undefined...color:undefined
不过第二行和原生的bind还是有点区别的,这里还是记住了之前的bind的对象,原生的不知道为啥是undefined
面试题
实现es5中的bind方法,使得狼蚁网站SEO优化的代码输出suess
function Animal(name,color){ this.name = name; this.color = color; } Animal.prototype.say = function(){ return `i am a ${this.color} ${this.name}` } const Cat = Animal.bind(null,"cat"); const cat = new Cat("white"); if(cat.say() === 'i am a white cat' && cat instanceof Cat && cat instanceof Animal){ console.log("suess") }
加上上面的bind实现,咦??没有出现suess??为什么?
分析一下代码,bind的第一个参数是null??null的时候应该默认为this,修改代码如下
Function.prototype.bind = function(obj){ const args = Array.prototype.slice.call(arguments,1);//保留bind时的参数 const that = this; const bound = function(){ const inArgs = Array.prototype.slice.call(arguments);//执行bind的函数时的参数 const newArgs = args.concat(inArgs);//组装参数 const bo = obj || this; that.apply(bo,newArgs);//执行bind的函数 } //继承prototype--寄生组合式继承 function F(){}; F.prototype = that.prototype; bound.prototype = new F(); return bound; }
输出suess
完美~~~
撒花~~~
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对狼蚁SEO的支持。
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指