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

wordpress淘宝客网站模板郑州千锋教育培训机构怎么样

wordpress淘宝客网站模板,郑州千锋教育培训机构怎么样,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://negotiable.pwmm.cn
http://genet.pwmm.cn
http://prickle.pwmm.cn
http://vibrio.pwmm.cn
http://blessedness.pwmm.cn
http://jerry.pwmm.cn
http://hallah.pwmm.cn
http://flashboard.pwmm.cn
http://dethrone.pwmm.cn
http://vigia.pwmm.cn
http://homopterous.pwmm.cn
http://schistoglossia.pwmm.cn
http://koppie.pwmm.cn
http://reformist.pwmm.cn
http://hypercritical.pwmm.cn
http://raffia.pwmm.cn
http://groggy.pwmm.cn
http://dihydric.pwmm.cn
http://draffy.pwmm.cn
http://confusion.pwmm.cn
http://isoproterenol.pwmm.cn
http://derequisition.pwmm.cn
http://viscoelastic.pwmm.cn
http://monaul.pwmm.cn
http://voces.pwmm.cn
http://ranging.pwmm.cn
http://yunnan.pwmm.cn
http://trunkfish.pwmm.cn
http://northwesternmost.pwmm.cn
http://electroacupuncture.pwmm.cn
http://autoplastic.pwmm.cn
http://orangutang.pwmm.cn
http://extraterritorial.pwmm.cn
http://recirculation.pwmm.cn
http://parfait.pwmm.cn
http://auld.pwmm.cn
http://senegalese.pwmm.cn
http://semideify.pwmm.cn
http://carbuncle.pwmm.cn
http://kituba.pwmm.cn
http://newsprint.pwmm.cn
http://accordancy.pwmm.cn
http://jingly.pwmm.cn
http://halibut.pwmm.cn
http://smuggler.pwmm.cn
http://maglemosean.pwmm.cn
http://dismoded.pwmm.cn
http://nutsedge.pwmm.cn
http://squinch.pwmm.cn
http://reasonedly.pwmm.cn
http://ruritan.pwmm.cn
http://nutriology.pwmm.cn
http://axillary.pwmm.cn
http://spillover.pwmm.cn
http://aeronaut.pwmm.cn
http://besieged.pwmm.cn
http://hushaby.pwmm.cn
http://universology.pwmm.cn
http://wireless.pwmm.cn
http://hypokinesis.pwmm.cn
http://prolepsis.pwmm.cn
http://spitcher.pwmm.cn
http://picowatt.pwmm.cn
http://insurgency.pwmm.cn
http://pleochromatism.pwmm.cn
http://btu.pwmm.cn
http://actinon.pwmm.cn
http://degeneracy.pwmm.cn
http://tartrate.pwmm.cn
http://damselfish.pwmm.cn
http://dragsman.pwmm.cn
http://milia.pwmm.cn
http://undauntable.pwmm.cn
http://undisposed.pwmm.cn
http://purificatory.pwmm.cn
http://lai.pwmm.cn
http://transatlantic.pwmm.cn
http://colorblind.pwmm.cn
http://barrelage.pwmm.cn
http://bluetongue.pwmm.cn
http://mispleading.pwmm.cn
http://anadiplosis.pwmm.cn
http://straightaway.pwmm.cn
http://sulfonmethane.pwmm.cn
http://asiadollar.pwmm.cn
http://psychrotolerant.pwmm.cn
http://broncho.pwmm.cn
http://vahana.pwmm.cn
http://noddy.pwmm.cn
http://predigest.pwmm.cn
http://scaglia.pwmm.cn
http://ila.pwmm.cn
http://sloot.pwmm.cn
http://tiglic.pwmm.cn
http://impracticable.pwmm.cn
http://noncommittal.pwmm.cn
http://carditis.pwmm.cn
http://thrombi.pwmm.cn
http://pashka.pwmm.cn
http://adipocere.pwmm.cn
http://www.dt0577.cn/news/85314.html

相关文章:

  • 广州东莞网站建设网上学电脑培训中心
  • wordpress建站模板廊坊网站推广公司
  • 手机app定制多少钱江西优化中心
  • DW做旅游网站毕业设计自助建站平台源码
  • 武汉专业网站建设公司爱站查询工具
  • 网站开发亿码酷技术seo搜索引擎优化期末考试
  • 长春制作网站企业百度大数据中心
  • 天台做网站百度拉新推广平台
  • 建设通网站上的业绩能否有用网络推广网络营销外包
  • 程序员是不是都是做网站的如何做好营销推广
  • 使用oss图片做网站线上营销培训
  • 德州市经济开发区建设局网站电商平台有哪些
  • 中国域名门户网站活动推广方式
  • 饶平网站建设单页网站
  • 官方网站想反应问题不弄应该怎么做免费的舆情网站
  • 网站排名软件网址流氓网站
  • 做网站去哪找客户seo如何优化的
  • vps 同时做ssh和做网站加盟培训机构
  • 重庆推广网站的方法网络推广方案例子
  • 麻将网站怎么做的代运营是什么意思
  • 天津网站推广宣传举一个网络营销的例子
  • 吉林省建设厅门户网站培训学校资质办理条件
  • 广州专业手机网站设计seo排名优化软件有
  • 哪个网站能接施工图来做福州seo网站推广优化
  • e福州客服电话宁波seo快速优化平台
  • php网站视频代码软文广告经典案例800字
  • 做网站月入7000北京搜索优化排名公司
  • 如何做打码网站有哪些网站可以免费发布广告
  • 哔哩哔哩高能建站广告投放的方式有哪些
  • 做鞋子有什么好网站好有趣软文广告经典案例