首页 > 作文

关于 JS this

更新时间:2023-04-07 06:24:49 阅读: 评论:0

关于 js this

1. this 与 普通函数

普通函数 this 永远指向它的调用对象

var obj = {    a:10,    b:{        a:20,        fn:function(){            console.log(this.a);        }    }}obj.b.fn(); //=> 20

对象 b 调用 fn 函数 this.a 为 20。

var obj2 = {    a:10,    b:{        fn:function(){            console.log(this.a);        }    }}obj2.b.fn(); //=> undefined

对象 b 中无 a 字段, this.a 为 undefined。

var obj3 = {    a:10,    b:{        a:20,        fn:function(){            console.log(this);            console.log(this.a);        }    }}var j = obj3.b.fn;j(); //=> window, undefined

这里直接获取里fn对象,没有通过对象去调用。非严格模式下,this 默认指向全局对象window。

var obj4 = {    a:10,    b:{        a:20,        fn:function(){            "u strict";            console.log(this);            console.log(this.a);        }    }}var g = obj4.b.fn;g(); //=> undefined, typeerror: cannot read property 'a' of undefined

而严格模式下, this 为 undefined。

2. this 与 bind、call、apply

function.bind(thisarg[, arg1[, arg2[, …]]])最简单的用法是创建一个函数,不论怎么调用,这个函数都有同样的this值。
function.call(thisarg, arg1, arg2, …)、function.apply(thisarg, [argsarray]) call、appl团队口号比较霸气的y 的作用类似都是为调用函数指定this。

function fn() {    return this.ur;}console.log(fn.call({ ur: "li" })); //=> liconsole.log(fn.apply({ ur: "wang" })); //=> wangvar bfn = fn.bind({ ur: "gao" });console.log(bfn()); //=> gaoconsole.log(bfn.call({ ur: "liu" })); //=> gaoconsole.log(bfn.apply({ ur: "liu" })); //=> gaoconsole.log(bfn.bind({ ur: "liu" })()); //=> gaovar obj = { ur: 'z周瑜hang', f: fn, g: bfn };console.log(obj.f(), obj.g()); //=>  zhang, gao

对 bind 产生的函数使用,再使用 call、apply、bind, 指向的 this 仍然是初次bind的值。

function list() {    return array.prototype.slice.call(arguments);}console.log(list(1, 2, 3)); //=> [1, 2, 3]function list2(){    if(arguments){        arguments.__proto__.slice = array.prototype.slice;        return arguments.slice();    }}console.log(list2(4, 5, 6)); //=> [4, 5, 6]var leading37list = list.bind(null, 37);console.log(leading37list()); //=> [37]console.log(leading37list(1, 2, 3)); //=> [37, 1, 2, 3]

在函数代码中,特殊对象 arguments 无需明确指出参数名,就能访问它们。arguments 自带 length 属性, array.prototype.slice.call(arguments) 可以理解成 arguments.slice()

3. this 与 箭头函数

由于箭头函数不绑定this,它会捕获其所在(即定义的位置)上下文的this值, 作为自己的this值。

var obj = {    a: 10,    b: function () {        console.log(this.a, this)    },    c: () => console.log(this.a, this),}obj.b(); //=> 10, object{...}obj.c(); //=> undefined, window{...} obj.c.apply({ a: 'apply' }); //=> undefined, window{...} obj.c.call({ a: 'call' }); //=> undefined, window{...} obj.c.bind({ a: 'bind' })(); //=> undefined, window{...} 

call() 、 apply() 、 bind() 方法对于箭头函数来说只是传入参数,对它的 this 毫无影响。

function fn() {数学教研组活动记录    this.a = 20;    this.b = () => console.log(this.a, this);}console.log((new fn()).b()) //=> 20, fn{a: 20, b: ƒ}

箭头函数不会创建自己的this,它只会从自己的作用域链的上一层继承this。

var lam = (...r) => {     console.log(r); }lam(1,2,3); //=> array : [1, 2, 3]

箭头函数没有argum全国大学ents,取而代之用rest参数…解决。

var b = ()=>{  value:1;}var b = new b(); //=> typeerror: b is not a constructor

箭头函数作为匿名函数,没有广东省高考报名构造函数,不能使用new。

4. this 与 return

如果返回值是 引用类型 对象,那么this指向的就是那个返回的对象;
如果返回值是 非引用类型 对象那么this还是指向函数的实例。

4.1 返回引用对象

function fn()  {      this.ur = '123';      return {};  }var a = new fn;  console.log(a.ur); //=> undefined

4.2 返回function对象

function fn()  {      this.ur = '123';      return function(){};}var a = new fn;  console.log(a.ur); //=> undefined

4.3 返回数字,值对象

function fn()  {      this.ur = '123';      return 123;}var a = new fn;  console.log(a.ur); //=> 123

4.4 返回undefined

function fn()  {      this.ur = '123';      return undefined;}var a = new fn;  console.log(a.ur); //=> 123

4.5 返回null

function fn()  {      this.ur = '123';      return null;}var a = new fn;  console.log(a.ur); //=> 123

本文发布于:2023-04-07 06:24:48,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/809a13b3f7ff16032dbd4365e000b2f5.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:关于 JS this.doc

本文 PDF 下载地址:关于 JS this.pdf

标签:函数   对象   箭头   自己的
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图