美文网首页JavaScript 基础
JS基础 - this 到底指向哪

JS基础 - this 到底指向哪

作者: MrWelson | 来源:发表于2020-07-07 18:05 被阅读0次

函数上下文 (this 指向)看这一篇就够了

  1. 简单调用,函数直接圆括号执行,函数上下文是 window(this 指向 window);

    var number = 1111
    function getNumber(){
        this.number = 2222
        console.log(this.number)
    }
    getNumber() // '2222', this 指向 window
    console.log(number) // '2222' number 的值被改变
    
  2. 函数(非箭头函数)作为对象的方法,对象最后打点调用 obj.fn() ,那么函数上下文是这个对象 obj。

    var name = 'jerry'
    function say(){
        console.log(this.name)
    }
    var obj = {
        name: 'tom',
        say: say
    }
    obj.say() // 'tom', this 指向 obj
    say() // 相当于 window.say(), this 指向 window
    
  3. 计时器的回调函数中的上下文是 window

    var name = 'jerry'
    function say(){
     setTimeout(function(){
            console.log(this.name)
     }, 1000)
    }
    var obj = {
        name: 'tom',
        say: say
    }
    obj.say() // 'jerry', 定时器执行,this 指向 window
    
  4. call & apply 将 this 指向 第一个参数

    function say(){
     console.log(this.name)
    }
    var obj = {
        name: 'tom',
        say: say
    }
    say.call(obj, 1,2,3,4)
    say.apply(obj, [1,2,3,4])
    
  5. bind 方法将 this 永久绑定到第一个参数

    function fn(){
      return this.name;
    }
    var g = fn.bind({ name: "tom" });
    console.log(g()); // tom
    
    var h = g.bind({ name: "jerry" }); // bind只生效一次!
    console.log(h()); // tom
    
  6. 箭头函数 this 指向其创建时所处上下文。在全局代码中,它将被设置为全局对象

箭头函数本身没有构造函数,是不能使用 new 关键字创建实例的

 var foo = () => this;
 var obj = { foo: foo };
 var obj2 = {
   foo: () => this
 }
 console.log(obj.foo()); // Window
 console.log(obj2.foo()); // Window

 var obj = {
   bar: function() {
     var x = () => this;
     return x;
   }
 };
 obj.bar()() // obj 
  1. 构造函数: new 运算符

    new 运算符执行的函数叫做构造函数,也可以是一个类,new 函数返回的结果就是一个对象。构造函数中的 this 指向这个被创建的新对象。

    new 运算符执行规则:

    1. new 运算符执行函数的时候,函数内部默认创建一个空对象 People
    2. 函数上下文指向这个对象 People
    3. 执行函数体 (this.name, this.age ...)
    4. 构造函数默认返回这个对象本身 People {name: 'jerry', age: 20}
    function Person() {
        this.name = 'jerry'
        this.age = 20
        this.sayHi = function(){
          console.log(this)
        }
    }
    var obj = new Person(); // new执行函数, this 指向构造函数 Person
    console.log(obj); // Person
    obj.sayHi()
    

相关文章

  • JS基础 - this 到底指向哪

    函数上下文 (this 指向)看这一篇就够了 简单调用,函数直接圆括号执行,函数上下文是 window(this ...

  • this到底指向哪?

    老生常谈的问题了,之前一直有些模糊,这次争取一次写清楚。 指向只与调用有关 不论代码多么复杂我们只关心到底是谁最后...

  • 学习总结目录

    javascript基础 js数据类型面向对象继承js的this指向自己实现call,apply,bindjs事件...

  • 关于js中this指向的那些事

    this是JS中的关键字,this的指向中函数定义时确定不了,只有 函数指向的时候才能确定this到底指向谁,th...

  • js中this到底指向谁

    什么是this JavaScript中的this是什么?定义:this是包含它的函数作为方法被调用时所属的对象。 ...

  • 面试官问:JS的this指向

    前言 面试官出很多考题,基本都会变着方式来考察this指向,看候选人对JS基础知识是否扎实。读者可以先拉到底部看总...

  • 面试官问:JS的this指向

    前言 面试官出很多考题,基本都会变着方式来考察this指向,看候选人对JS基础知识是否扎实。读者可以先拉到底部看总...

  • JS this指向

    一、js中的四种调用模式s 构造函数调用:new Foo(); 对象方法调用:o.method(); 函数直接调用...

  • js this指向

    this指向调用该函数的对象 在函数中this到底取何值,是在函数真正被调用执行的时候确定的,函数定义的时候确定不...

  • JS this指向

    首先,请牢记以下三点1. this指向的,只可能是对象!2.this指向谁,不取决于this写在哪!而是取决于函数...

网友评论

    本文标题:JS基础 - this 到底指向哪

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