美文网首页
Js中的this

Js中的this

作者: kkyeer | 来源:发表于2019-01-10 19:50 被阅读0次

1.处于window上下文或者不在任何function中时,this指向window,不管当前是否处于use strict状态
2.在一个function中调用this时,要看function如何被调用
①非strict mode,直接调用方法时,this指向window
function f1(){
return this;
}
console.log(f1()===window);//true
let f2=()=>{
return this;
}
console.log(f2()===window)//true
②strict mode,因为strict mode 规定,若没有在调用时显式指定this,则this为undefined
function f3(){
'use strict';
return this;
}
console.log(f3())//undefined
然而,在使用箭头表达式时,隐式的指定调用为this,即window
let f4=()=>{
'use strict';
return this;
}
console.log(f4()===window)// true
3.调用时需要指定this时,使用call或者apply(仅在函数是function直接定义时使用,箭头函数无此效果)
let custom={desc:'custom'}
let desc = 'window'
function normalWhatsThis(){
return this.desc;
}
let arrowWhatsThis=()=>{
return this.desc;
}
normalWhatsThis()//undefined
normalWhatsThis.call(custom)//"custom"
normalWhatsThis.apply(custom)//"custom"
// 箭头表达式没有这个效果
arrowWhatsThis()//undefined
arrowWhatsThis.call(custom)//undefined
arrowWhatsThis.apply(custom)//undefined
4.在调用函数时,使用bind可以绑定this
let foo={
desc:'foo'
}
let bar={
desc:'bar',
printMe : function(){
console.log(this.desc);
}
}
bar.printMe();// bar
let printFoo=bar.printMe.bind(foo);
printFoo()//foo
5.箭头函数调用中,this指向当前未关闭的语法上下文的this(enclosing lexical context's this)
let global=this;
let test=(()=>this);
console.log(test()===this)//true
*使用call,bind或者apply调用箭头函数时,第一个入参会被忽略
let custom2 = {
desc2:'custom'
}
let desc2 = 'window'

function arrowWhatsThis(){
return console.log(this.desc2);
}
arrowWhatsThis()// undefined
arrowWhatsThis.call(custom) // undefined
6.如果函数本身作为对象(object)的成员,则函数中的this指向方法被调用的对象
console.log('--------1-------');
let obj1={
a:3,
fn:function(){
console.log(this.a);
console.log(this===obj1);
}
}
obj1.fn()
console.log('--------------2-------');
let indiFn1=function(){
console.log(this.a);
console.log(this===obj2);

}
let obj2={
a:4,
fn:indiFn1
}
let obj4={
a:5,
fn:{}
}
obj2.fn();
obj4.fn=indiFn1;
obj4.fn();
console.log('-----------3---------------');
let indiFn2=()=>{
console.log(this.a);
console.log(this===obj2);

}
let obj3={
a:5,
fn:indiFn2
}
obj3.fn()
输出:
--------1-------
3
true
--------------2-------
4
true
5
false
-----------3---------------
undefined
false
7.对象原型链中的this
let o = {
f:function(){
return this.a+this.b;
}
}
let p = Object.create(o);
p.a=3;
p.b=4;
console.log(p.f());//7

  • getter和setter的道理一致
    8.构造器中的this
    当一个方法被用作构造器,即方法是通过new关键词调用时,this被绑定为构造的新对象
    function fn(){
    this.c=99
    }
    let obj = new fn();
    console.log(obj.c);//99
    9.作为dom事件的handler
    Dom事件的handler为function,则其中的this指向抛出事件的对象,即e.currentTarget
    <button id='btn' onclick="alert(this.tagName)">ClickMe</button>//BUTTON

引用:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

相关文章

  • JavaScript 04 (do...while循环/for

    js循环,js循环嵌套,js do...while循环,js的for循环,js中的break,js中的contin...

  • iOS原生&JS交互

    OC 获取js中的key OC调用js方法 JS调用OC中不带参数的block JS调用OC中带参数的block ...

  • JS 对象

    JS对象 JS对象的意义和声明 在JS中,对象(OBJECT)是JS语言的核心概念,也是最重要的数据类型。在JS中...

  • 单引号和双引号

    JS中 js中单引号和双引号的区别(html中属性规范是用双引号,js中字符串规定是用单引号)(js中单引号区别和...

  • js中的this

    一句话,call的第一个参数 看几个例子例1. 这里的this是什么?不要靠猜,是call的第一个参数,不知道去看...

  • js 中的this

    首先js中函数的this在函数被调用时总是指向一个对象(this对象是在运行时基于函数的执行环境绑定的) 然后 它...

  • JS中的this

    JS中的this 众所周知,JS中this的代表的是当前函数调用者的上下文。JS是解释性的动态类型语言,函数在调用...

  • js中的!!

    !!一般用来将后面的表达式强制转换为布尔类型的数据(boolean),也就是只能是true或者false。 var...

  • js中的this

    目标:js中this的指向? 问题的引出 指出this指向什么 js中函数的三种调用形式 func(p1, p2)...

  • JS中的this

    初学JavaScript经常被this绕晕,所以我总结一下JavaScript中的this。首先声明本文讨论的是非...

网友评论

      本文标题:Js中的this

      本文链接:https://www.haomeiwen.com/subject/lvhzrqtx.html