Javascript中的this指向问题

一、普通函数调用(默认绑定)

// 非严格模式
var name = 'window';
var doSth = function(){
    console.log(this.name);
}
doSth(); // 'window'

window.doSth并不是调用的才指向window,而是因为上述代码在ES5中,全局变量是挂载在顶层对象的(浏览器是window),但若改为ES6,则结果不同。

let name = 'window';
let doSth = function(){
    console.log(this === window);
    console.log(this.name);
}
doSth(); // true   undefinded

let没有给顶层对象中(浏览器是window)添加属性,window.name和window.doSth都是undefined

 

当使用严格模式的时候,普通函数的this则不同,表现为undefined。

// 严格模式
'use strict'
var name = 'window';
var doSth = function(){
    console.log(typeof this === 'undefined');
    console.log(this.name);
}
doSth(); // true,// 报错,因为this是undefined

上述表现即是默认绑定  可在《你不知道的JavaScript》中查阅。

 

二、对象中的方法调用模式

var name = 'window';
var doSth = function(){
    console.log(this.name);
}
var student = {
    name: '恒若',
    doSth: doSth,
    other: {
        name: 'other',
        doSth: doSth,
    }
}
student.doSth(); // '恒若'
student.other.doSth(); // 'other'
// 用call类比则为:
student.doSth.call(student);
// 用call类比则为:
student.other.doSth.call(other);

若将对象中的函数赋值成一个变量,则会变成普通函数(默认绑定)

var studentDoSth = student.doSth;
studentDoSth(); // 'window'
// 用call类比则为:
studentDoSth.call(undefined);

 

三、call、apply、bind调用

可先阅读MDN文档   https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/call   

语法

fun.call(thisArg, arg1, arg2, ...)

thisArg:fun函数运行时指定的this值。this并不一定会真正指向函数执行时的this值,如果这个函数在非严格模式下,指定为null和undefined的this值会指向全局对象(浏览器为window。)

若值为原始值(数字,字符串,布尔值)的this会指向该值的自动包装对象

arg1、arg2:参数列表

返回值:返回调用方法的返回值。,若没有返回值,则返回undefined。    apply与call类似,只是参数不一样,apply的参数是数组。   bind与call、apply类似,只是返回值是新函数。

说白了,call就是改变函数中的this指向thisArg,并执行函数。严格模式下,thisArg的原始值是值类型,也就是不会被包装成对象。

var doSth = function(name){
    console.log(this);
    console.log(name);
}
doSth.call(2, '恒若'); // Number{2}, '恒若'
var doSth2 = function(name){
    'use strict';
    console.log(this);
    console.log(name);
}
doSth2.call(2, '恒若'); // 2, '恒若'

 

四、构造函数调用

function Student(name){
    this.name = name;
    console.log(this); // {name: '恒若'}
    // 相当于返回了
    // return this;
}
var result = new Student('恒若');

new操作符调用函数:

1.创建了一个全新的对象。
2.这个对象会被执行[[Prototype]](也就是__proto__)链接。
3.生成的新对象会绑定到函数调用的this。
4.通过new创建的每个对象将最终被[[Prototype]]链接到这个函数的prototype对象上。
5.如果函数没有返回对象类型Object(包含Functoin, Array, Date, RegExg, Error),那么new表达式中的函数调用会自动返回这个新的对象。

即new操作符调用时,this指向新生成的对象。(new调用时的返回值没有显式返回或者函数,才是返回新生成的对象。)

function Student(name){
    this.name = name;
    // return function f(){};
    // return {};
}
var result = new Student('恒若');
console.log(result); {name: '恒若'}
// 如果返回函数f,则result是函数f,如果是对象{},则result是对象{}

 

五、原型链调用

 

function Student(name){
    this.name = name;
}
var s1 = new Student('恒若');
Student.prototype.doSth = function(){
    console.log(this.name);
}
s1.doSth(); // '恒若'

其实就是对象上的方法调用,指向生成的新对象。若该对象继承自其它对象,会通过原型链查找。

ES6中的class写法(语法糖):

class Student{
    constructor(name){
        this.name = name;
    }
    doSth(){
        console.log(this.name);
    }
}
let s1 = new Student('恒若');
s1.doSth();

 

六、箭头函数调用

1.箭头函数与普通函数的重要区别:

 1、没有自己的thissuperargumentsnew.target绑定。

2、不能使用new来调用。

3、没有原型对象。

4、不可以改变this的绑定。

5、形参名称不能重复。

箭头函数中没有this绑定,必须通过查找作用域链来决定其值。如果箭头函数被非箭头函数包含,则箭头函数的this指向最近一层的非箭头函数的this,否则this的值被设置为全局对象。

 

var name = 'window';
var student = {
    name: '恒若',
    doSth: function(){
        // var self = this;
        var arrowDoSth = () => {
            // console.log(self.name);
            console.log(this.name);
        }
        arrowDoSth();
    },
    arrowDoSth2: () => {
        console.log(this.name);
    }
}
student.doSth(); // '恒若'
student.arrowDoSth2(); // 'window'

无法通过call、apply、bind绑定箭头函数的this,但可以通过绑定箭头函数的上层的普通函数的this。

var student = {
    name: '恒若',
    doSth: function(){
        console.log(this.name);
        return () => {
            console.log('arrowFn:', this.name);
        }
    }
}
var person = {
    name: 'person',
}
student.doSth().call(person); // '恒若'  'arrowFn:' '恒若'       看方法调用的执行位置
student.doSth.call(person)(); // 'person' 'arrowFn:' 'person'

 

 

posted @ 2019-11-30 21:46  恒若  阅读(204)  评论(0)    收藏  举报