当前位置: 首页 > news >正文

在线教育网站建设公司网络营销岗位技能

在线教育网站建设公司,网络营销岗位技能,行业门户网站方案,哪些网站可以接设计的单子做理解this 在ES5中,this的指向始终坚持一个原理:“this永远指向最后调用它的那个对象”,切记这句话。下面看几个例子。 例一 var obj {name: zhangsan,say: function() {console.log(this.name);} }obj.say() // zhangsan 最基本的使用&am…

理解this

在ES5中,this的指向始终坚持一个原理:“this永远指向最后调用它的那个对象”,切记这句话。下面看几个例子。

例一

var obj = {name: 'zhangsan',say: function() {console.log(this.name);}
}obj.say() // zhangsan 

最基本的使用,函数say是obj调用的,函数中的this指向obj,所以打印的是zhangsan

例二

var name = 'lisi'
var obj = {name: 'zhangsan',say: function() {console.log(this.name);}
}var func = obj.say
func() // lisi 

把say赋给变量func,然后调用func,前面没有调用对象,那么此时的调用对象就是全局对象window,因此this指向window,所以打印lisi

例三

var name = 'lisi'
var obj = {name: 'zhangsan',say: function() {console.log(this.name);}
}window.obj.say() // zhangsan 

this永远指向最后调用它的那个对象,这里虽然前面加了个window,但是最后调用者还是obj,因此this指向obj。

可以看出:this的指向并不是在创建的时候就可以确定的。在ES5中,this永远指向最后调用它的那个对象

例四

var name = 'lisi'var obj = {name: 'zhangsan',say: function() {console.log(this.name); // zhangsanfunc()function func() {console.log(this.name); // lisi}},}obj.say() 

say的调用者是obj,say函数内部的this就指向obj,因此第一次打印zhangsan。再调用func,此时func的调用者是window,func函数内部的this就指向window,因此第二次打印lisi

如何改变this指向

改变this指向有以下几种方法:

  • 使用ES6的箭头函数
  • 把this存一下然后调用:_this=this
  • 使用apply、call、bind改变this指向

箭头函数

ES6中的箭头函数可以避免ES5中很多this的坑。箭头函数中的this始终指向函数定义时的this,而非执行时。箭头函数中没有this绑定,必须通过查找作用域链来决定其值。如果箭头函数被非箭头函数包含,则this绑定的是最近一次非箭头函数的this

例五

var name = 'lisi'var obj = {name: 'zhangsan',func1: function() {console.log(this.name);},func2: function() {setTimeout(function() {this.func1()}, 1000)}
}obj.func2() // this.func1 is not a function 

如上代码,调用func2报错,因为最终调用setTimeout的对象是window,函数里的this指向window,而window中并没有func1,所以报错。

这里补充个知识点:匿名函数的this永远指向window。this永远指向最后调用它的那个对象。我们来找找最后调用匿名函数的对象是哪个,很尴尬,匿名函数没有名字,所以是没有办法被其他对象调用的,所以只能在window下执行。因此匿名函数的this始终指向window。

var name = 'lisi'var obj = {name: 'zhangsan',func1: function() {console.log(this.name);},func2: function() {// 使用箭头函数setTimeout(() => {this.func1()}, 1000)}
}obj.func2() // zhangsan 

使用箭头函数即可解决问题,此时this就是func2函数中的this,而func2是obj调用的,因此this实际指向obj。

_this=this

简单把this存一下即可

例六

var name = 'lisi'var obj = {name: 'zhangsan',func1: function() {console.log(this.name);},func2: function() {// this存一下在匿名函数里使用var _this = thissetTimeout(function() {_this.func1()}, 1000)}
}obj.func2() // zhangsan 

apply、call

apply和call都是用来改变this指向的,作用相同,用法稍微有点不同。

MDN中apply定义如下:

apply() 方法调用一个函数, 其具有一个指定的this值,以及作为一个数组(或类似数组的对象)提供的参数

用法:

func.apply(thisArg, ArrayArg) 

call定义如下:

call() 方法使用一个指定的 this 值和单独给出的一个或多个参数来调用一个函数。

用法:

func.call(thisArg, arg1, arg2, ...) 

详细用法见MDN。

例七

var name = 'lisi'var obj = {name: 'zhangsan',say: function(arg) {console.log(this.name, arg);}
}var func1 = obj.say // lisi 1
func1(1) 

此时this指向window,打印lisi 1。如果我们现在仍想要this指向obj,那么就可通过apply改变this:

var name = 'lisi'var obj = {name: 'zhangsan',say: function(arg) {console.log(this.name, arg);}
}var func1 = obj.say // zhangsan 2
func1.apply(obj, [2]) 

通过apply改变this指向,指向obj。然后通过一个数组传入调用时的参数。这就是apply用法的简单实例。call用法与其类似,只不过不是通过数组传参,而是需要显式传入多个参数。

var name = 'lisi'var obj = {name: 'zhangsan',say: function(arg) {console.log(this.name, arg);}
}var func1 = obj.say // zhangsan 3
func1.call(obj, 3) 

手动实现apply、call

理解了this、this指向、apply、call后我们可以思考一下如何手动实现apply、call。

手动实现apply

Function.prototype._apply = function(context) {// context是要绑定的this// this是调用的函数即func1if (typeof this !== "function") {throw new Error('只能在函数上调用')return}context = context || windowlet result = null// 把需要执行的函数赋给context,由context调用即可context.fn = thisif (arguments[1]) {result = context.fn(...arguments[1])} else {result = context.fn()}delete context.fnreturn result
}var name = 'lisi'var obj = {name: 'zhangsan',say: function(arg) {console.log(this.name, arg);}
}var func1 = obj.say // zhangsan 1
func1._apply(obj, [1]) 

总结步骤如下:

  • 判断调用对象是否为函数,即使是定义在函数的原型上的,但是可能出现使用 call 等方式调用的情况。* 判断传入上下文对象是否存在,如果不存在,则设置为 window 。* 将函数作为上下文对象的一个属性。* 判断参数值是否传入* 使用上下文对象来调用这个方法,并保存返回结果。* 删除刚才新增的属性* 返回结果手动实现call

Function.prototype._call = function(context) {// context是要绑定的this// this是调用的函数即func1if (typeof this !== "function") {throw new Error('只能在函数上调用')return}context = context || windowconst arg = Array.from(arguments).splice(1)let result = null// 把需要执行的函数赋给context,由context调用即可context.fn = thisresult = context.fn(...arg)delete context.fnreturn result
}var name = 'lisi'var obj = {name: 'zhangsan',say: function(arg) {console.log(this.name, arg);}
}var func1 = obj.say // zhangsan 2
func1._call(obj, 2) 

最后

为大家准备了一个前端资料包。包含54本,2.57G的前端相关电子书,《前端面试宝典(附答案和解析)》,难点、重点知识视频教程(全套)。



有需要的小伙伴,可以点击下方卡片领取,无偿分享


文章转载自:
http://inclinable.c7500.cn
http://nystagmus.c7500.cn
http://covered.c7500.cn
http://prepayment.c7500.cn
http://andalusite.c7500.cn
http://sunderance.c7500.cn
http://exosphere.c7500.cn
http://hematophagous.c7500.cn
http://oriented.c7500.cn
http://rotifer.c7500.cn
http://floss.c7500.cn
http://heaping.c7500.cn
http://osage.c7500.cn
http://raiser.c7500.cn
http://waterproof.c7500.cn
http://feature.c7500.cn
http://sacciform.c7500.cn
http://pipet.c7500.cn
http://mite.c7500.cn
http://symbolatry.c7500.cn
http://roentgenopaque.c7500.cn
http://bored.c7500.cn
http://irrotationality.c7500.cn
http://ramie.c7500.cn
http://bats.c7500.cn
http://condignly.c7500.cn
http://sensed.c7500.cn
http://curarize.c7500.cn
http://rumpbone.c7500.cn
http://jesuitism.c7500.cn
http://whiggism.c7500.cn
http://pytheas.c7500.cn
http://troutling.c7500.cn
http://vouch.c7500.cn
http://roo.c7500.cn
http://decoy.c7500.cn
http://laniferous.c7500.cn
http://fugato.c7500.cn
http://overshirt.c7500.cn
http://vertimeter.c7500.cn
http://waterspout.c7500.cn
http://hapaxanthous.c7500.cn
http://prohormone.c7500.cn
http://flakelet.c7500.cn
http://triturator.c7500.cn
http://calves.c7500.cn
http://ronyon.c7500.cn
http://intemerate.c7500.cn
http://prizefighter.c7500.cn
http://gossip.c7500.cn
http://tensility.c7500.cn
http://inductivity.c7500.cn
http://halberd.c7500.cn
http://nae.c7500.cn
http://nearsighted.c7500.cn
http://bacchanal.c7500.cn
http://decomposer.c7500.cn
http://lythraceous.c7500.cn
http://spaniard.c7500.cn
http://insensitive.c7500.cn
http://heos.c7500.cn
http://dysphasic.c7500.cn
http://grouper.c7500.cn
http://ouzel.c7500.cn
http://taipei.c7500.cn
http://deeryard.c7500.cn
http://recondition.c7500.cn
http://thermic.c7500.cn
http://aztecan.c7500.cn
http://washday.c7500.cn
http://pergamum.c7500.cn
http://dichroite.c7500.cn
http://tidewater.c7500.cn
http://chordate.c7500.cn
http://squawk.c7500.cn
http://pulverize.c7500.cn
http://larynges.c7500.cn
http://flabellate.c7500.cn
http://dynel.c7500.cn
http://corniness.c7500.cn
http://clericate.c7500.cn
http://neurohypophysis.c7500.cn
http://ataxy.c7500.cn
http://uncertainty.c7500.cn
http://sixain.c7500.cn
http://hamfooted.c7500.cn
http://grossularite.c7500.cn
http://alcaide.c7500.cn
http://tishri.c7500.cn
http://togoland.c7500.cn
http://postproduction.c7500.cn
http://epithelioma.c7500.cn
http://bloodsucker.c7500.cn
http://compadre.c7500.cn
http://tinglass.c7500.cn
http://claribel.c7500.cn
http://orthophoto.c7500.cn
http://remoulade.c7500.cn
http://ecclesiasticism.c7500.cn
http://misunderstanding.c7500.cn
http://www.zhongyajixie.com/news/82004.html

相关文章:

  • 宿迁哪里有做网站开发的石家庄seo网络优化的公司
  • 南通高端网站设计申请一个网站
  • 门户网站建设评标办法武汉seo系统
  • 福州做网站公司排名seo效果分析
  • 湖北建设厅网站上查询网络推广公司排行榜
  • 校园网站建设的用处学做网站培训班要多少钱
  • 泉州平台网站建设seo网络推广软件
  • 站长工具seo诊断我想在百度上做广告怎么做
  • 郑州网站设计哪家公司好外链代发
  • 郑州网络推广哪个好seo导航站
  • wordpress做公司网站云建站
  • 长沙优化网站建设百度搜索引擎推广步骤
  • 小县城做房地产网站哈尔滨百度推广公司
  • 长沙网站建设 网站设计站长工具seo综合查询引流
  • 想换掉做网站的公司百度推广培训机构
  • 做模块高考题的网站深圳网络推广网站推广
  • 网页设计开发招聘广州网站seo
  • 网站维护提示怎么做网络舆情管理
  • 帮别人做网站市场价app推广平台有哪些
  • 做电商有哪些网站宁波seo关键词排名
  • go做后端的网站快速收录网
  • 懂做网站怎么赚钱公司网站设计方案
  • 义乌网站公司app下载推广
  • 有服务器域名源码怎么做网站平台裤子seo关键词
  • 怎么开通微信小程序卖东西江北seo
  • 一家公司做网站需要什么资料外链链接平台
  • 深圳营销网站建设网站seo外包价格
  • 重庆制作网站公司企业管理培训课程费用
  • 如何申请域名空间seo整站优化一年价格多少
  • 怎么做网站免费优化软文推广文章范文1000