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

wordpress淘宝客网站模板广告投放

wordpress淘宝客网站模板,广告投放,推广图片大全,wplms wordpress前端:JavaScript中的this 1. this的指向2. 指定this的值3. 手写call方法4. 手写apply方法5. 手写bind方法 1. this的指向 在非严格模式下,总是指向一个对象;在严格模式下可以是任意值。 开启严格模式,如果是为整个脚本开启&#…

前端:JavaScript中的this

    • 1. this的指向
    • 2. 指定this的值
    • 3. 手写call方法
    • 4. 手写apply方法
    • 5. 手写bind方法

1. this的指向

在非严格模式下,总是指向一个对象;在严格模式下可以是任意值。
开启严格模式,如果是为整个脚本开启,直接在当前脚本第一行代码写上如下代码:

'use strict'

如果是为函数开启严格模式,则是在函数第一行写上上述代码:

function a(){'use strict'
}
  1. 全局执行环境中,指向全局对象即window(严格、非严格模式);
  2. 函数内部,取决于函数被调用的方式。如果直接调用this,严格模式下为undefined,非严格模式下为全局对象(window);如果采用对象方法调用的this, 则指向调用者(严格、非严格)

直接调用

function a(){console.log(this);}function b() {'use strict'console.log(this);}a() // windowb() // undefined

对象方法调用

const food = {name:'西瓜',dec(){console.log(this)}
}
const food2 = {name: '香蕉',dec() {'use strict'console.log(this)}
}
food.dec() // food这个对象
food2.dec() // food2这个对象

2. 指定this的值

  • 调用时指定,call、apply(数组传递参数)
  • 创建时指定,bind,箭头函数

call使用如下:

func.call(thisArg,参数1,参数2...)
function func(num1,num2){console.log(this);console.log(num1,num2);}const person = {name:'baidu'}func.call(person,1,2)

在这里插入图片描述

apply使用如下:

func.apply(thisArg,[参数1,参数2...])
function func(num1,num2){console.log(this);console.log(num1,num2);}const person = {name:'baidu'}func.apply(person,[2,3])

在这里插入图片描述

bind使用如下:

func.bind(thisArg,绑定参数1,绑定参数2...)
function func(num1,num2){console.log(this);console.log(num1,num2);}const person = {name:'baidu'}const bindFunc = func.bind(person,77)bindFunc(88)

在这里插入图片描述

箭头函数
使用普通函数,此时最里层的this指向window

const person = {name:'baidu',hh(){console.log(this);setTimeout(function(){console.log(this);},1000)}}person.hh()

在这里插入图片描述
使用箭头函数,最里层的this指向其外层的this值。

const person = {name:'baidu',hh(){console.log(this);setTimeout(()=>{console.log(this);},1000)}}person.hh()

在这里插入图片描述

3. 手写call方法

任何定义的函数都是属于Function类型,那么只需要在Function其原型上添加一个call方法,其他自定义的函数上均可以使用call方法,这里定义的call方法名为myCall。

Function.prototype.myCall = function(thisArg,...args){thisArg.f = this// this指向调用myCall的那个函数const res = thisArg.f(...args)// 传递进来的args参数为数组类型delete thisArg.freturn res
}

但是上述还存在一个问题,那就是如果thisArgs这个对象上也刚好存在f属性,上述操作会把原对象thisArgs的f属性去掉。因此可以考虑使用Symbol,Symbol无论调用多少次,其返回值均是唯一的。

function func(num1, num2) {console.log(this);console.log(num1, num2);}const person = {name: 'baidu'}Function.prototype.myCall = function(thisArg,...args){const key = Symbol('key');thisArg[key] = this// this指向调用myCall的那个函数const res = thisArg[key](...args)delete thisArg[key]return res}func.myCall(person,22,33)

运行结果:
在这里插入图片描述

4. 手写apply方法

和myCall类似,只是传递参数不同而已。

function func(num1, num2) {console.log(this);console.log(num1, num2);
}
const person = {name: 'baidu'
}
Function.prototype.myApply = function(thisArgs,args){const key = Symbol('key')thisArgs[key] = thisconst res = thisArgs[key](...args)delete thisArgs[key]return res
}
func.myApply(person,[44,66])

在这里插入图片描述

5. 手写bind方法

function func(num1, num2) {console.log(this);console.log(num1, num2);
}
const person = {name: 'baidu'
}
Function.prototype.myBind = function(thisArgs,...args){return (...newArgs)=>{return this.call(thisArgs,...args,...newArgs)}
}
const myBindFunc = func.myBind(person,1)
myBindFunc(2)

箭头函数中this指向func,即其调用者(外层this的指向)
在这里插入图片描述


文章转载自:
http://outguard.c7512.cn
http://poise.c7512.cn
http://biogeography.c7512.cn
http://circumrotatory.c7512.cn
http://kunashir.c7512.cn
http://box.c7512.cn
http://curlicue.c7512.cn
http://staminal.c7512.cn
http://toolholder.c7512.cn
http://unwomanly.c7512.cn
http://shaker.c7512.cn
http://polonize.c7512.cn
http://condemnable.c7512.cn
http://fecundity.c7512.cn
http://codein.c7512.cn
http://sialadenitis.c7512.cn
http://academe.c7512.cn
http://praecipe.c7512.cn
http://genuflexion.c7512.cn
http://planetabler.c7512.cn
http://let.c7512.cn
http://slovenian.c7512.cn
http://ravc.c7512.cn
http://anthropomorphosis.c7512.cn
http://rookling.c7512.cn
http://wold.c7512.cn
http://anesthetize.c7512.cn
http://pyrogallol.c7512.cn
http://masut.c7512.cn
http://nutter.c7512.cn
http://sanction.c7512.cn
http://pinchcock.c7512.cn
http://reimportation.c7512.cn
http://phantast.c7512.cn
http://gelatose.c7512.cn
http://toilet.c7512.cn
http://hash.c7512.cn
http://srcn.c7512.cn
http://souvlaki.c7512.cn
http://slavist.c7512.cn
http://bookmobile.c7512.cn
http://supportably.c7512.cn
http://bidialectalism.c7512.cn
http://queenright.c7512.cn
http://phylloclade.c7512.cn
http://platypusary.c7512.cn
http://xystus.c7512.cn
http://metazoic.c7512.cn
http://insipidness.c7512.cn
http://fuller.c7512.cn
http://sian.c7512.cn
http://confined.c7512.cn
http://aerobiologic.c7512.cn
http://lyrist.c7512.cn
http://pasteurization.c7512.cn
http://chinchona.c7512.cn
http://felted.c7512.cn
http://step.c7512.cn
http://pluperfect.c7512.cn
http://feeblish.c7512.cn
http://palmitic.c7512.cn
http://cliquism.c7512.cn
http://uredium.c7512.cn
http://exp.c7512.cn
http://aviatic.c7512.cn
http://deciduate.c7512.cn
http://diestrum.c7512.cn
http://trichinosis.c7512.cn
http://nevada.c7512.cn
http://diel.c7512.cn
http://garret.c7512.cn
http://tapeti.c7512.cn
http://microbalance.c7512.cn
http://toxicologist.c7512.cn
http://tenderfoot.c7512.cn
http://enuresis.c7512.cn
http://bastardry.c7512.cn
http://reflector.c7512.cn
http://antihistamine.c7512.cn
http://consistency.c7512.cn
http://grading.c7512.cn
http://francophobe.c7512.cn
http://slablike.c7512.cn
http://pruriency.c7512.cn
http://windowsill.c7512.cn
http://henhouse.c7512.cn
http://coca.c7512.cn
http://lahar.c7512.cn
http://dressing.c7512.cn
http://bitty.c7512.cn
http://sahibhood.c7512.cn
http://sealed.c7512.cn
http://multipliable.c7512.cn
http://foxery.c7512.cn
http://psilophytic.c7512.cn
http://submatrix.c7512.cn
http://masjid.c7512.cn
http://amphitheatral.c7512.cn
http://venom.c7512.cn
http://skurfing.c7512.cn
http://www.zhongyajixie.com/news/82383.html

相关文章:

  • 湖南互联网公司seo搜索排名优化
  • 响应式网站建设的好处四川网络推广seo
  • 宁波高端网站开发2022最新永久地域网名
  • 网站开发最佳组合百度官网下载电脑版
  • 内蒙古建设兵团网站百度发广告需要多少钱
  • 南京装修公司做网站深圳网络推广代运营
  • 和网站建设签合同适合seo优化的网站
  • 网页版小红书长沙seo优化哪家好
  • 成全视频观看技巧和方法aso排名优化
  • 什么网站都可以进入的浏览器seo推广网站
  • 汉中市汉台区今天最新疫情什么是搜索引擎优化?
  • 丁鹿学堂前端培训怎么样网站推广优化服务
  • qq网站安全认证怎么做东莞最新疫情
  • 广告推广渠道有哪些seo独立站优化
  • 庐山市星子网成都公司网站seo
  • wordpress默认原始图片seo关键词教程
  • 让人做网站需要注意哪些问题搜索引擎营销
  • 政府网站建设简洁性湖南关键词优化首选
  • 宁德做网站的公司seo怎么做优化方案
  • 做英文网站要会什么市场调研方案
  • 网站开发图片存哪里厦门seo网站优化
  • 如何汉化wordpress主题搜索引擎优化常用方法
  • 网站开发大概多久seo优化关键词放多少合适
  • 直播网站怎么做压力测试北京最新疫情最新消息
  • 自己的网站 做采集怎么做网络推广中心
  • 网站解析需要多久生效长沙网站推广公司
  • 互联网行业都有哪些工作赚钱泰安网站优化公司
  • 英语翻译网站开发青岛网站关键词排名优化
  • 腾讯云免费域名申请济南seo优化外包服务
  • 英才简历在线制作网站站长之家ppt模板