修改this指向的目的:
1. call()方法
function fn(a,b){ console.log(this); console.log(a); //world console.log(b); //123 return "back"; } fn("world",123); //this指向window var res = fn.call("hello","world",123); //this指向hello console.log(res); //back
2. apply()方法
function fn(a,b){ console.log(this); console.log(a); //world console.log(b); //123 return "back"; } fn("world",123); //this指向window var res = fn.apply("hello",["world",123]); //this指向hello console.log(res); //back
3. bind()方法
function fn(a,b){ console.log(this); console.log(a); //world console.log(b); //123 return "back"; } fn("world",123); //this指向window var res = fn.bind("hello","world",123); console.log(res); //修改this指向之后的新函数 res(); //执行返回的新函数,this指向hello console.log(res == fn); //false
3种方法的区别:
标题
参数1
参数2(可省略)
返回值
call()
this的新指向
向函数中正常传递的实参(可多个)
原函数的返回值
apply()
this的新指向
数组(自动解析)
原函数的返回值
bind()
this的新指向
向函数中正常传递的实参(可多个)
修改this指向之后的新函数(需手动执行获取this)
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算