美文网首页
总结篇(三) -- 你知道 log 出来的 this 是什么么

总结篇(三) -- 你知道 log 出来的 this 是什么么

作者: bowen_wu | 来源:发表于2018-02-05 09:34 被阅读79次

概述

函数中的 this 是一个很重要的知识点,如果能清楚的知道各种场景下函数中 this 所代表的值,那么对于我们去理解库 | 框架是很有帮助的,也可以让我们去运用一些更加高级的思想

this

this 的使用情况大致分为两种

  • 函数中
  • 对象的方法中
this === call | apply | bind 的第一个参数

在判断 this 的值的时候,进行相应的转化是很有必要的

严格模式 + 非严格模式

示例一

'use strict'
function fn( a, b ){
    console.log( this )
}
fn( 1, 2 )

上述中的 this 会打印出什么呢?

fn( 1, 2 ) === fn.call( undefined, 1, 2 ) === fn.apply( undefined, [1, 2] )

答案

  • 严格模式 ==> this === undefined
  • 非严格模式 ==> this === undefined == 浏览器转化 ==> window

对象方法

示例二

let obj = {
    fn: function( a, b ){
        console.log( this )
    },
    child: {
        fn2: function(){
            console.log( this )
        }
    }
}
obj.fn( 1, 2 )
obj.child.fn2()

上述中的 this 会打印出什么呢?

obj.fn( 1, 2 ) === obj.fn.call( obj, 1, 2 ) === obj.fn.apply( undefined, [ 1, 2 ] )  ==> this === obj
obj.child.fn2() === obj.child.fn2.call( obj.child )  ==> this === obj.child

示例三

let arr = []
for( let i = 0; i < 3; i++ ){
    arr[ i ] = function(){
        console.log( this )
    }
}
let fn = arr[ 0 ]
arr[ 0 ]
fn()

上述中的 this 会打印出什么呢?

arr[ 0 ] ==> arr 对象{ 0: fn, 1: fn, 2: fn } ==> arr.0 ==> arr.0.call( this ) ==> this === arr
fn() ==> fn.call( undefined ) ==> this === undefined == 浏览器转化 ==> window

bind

bind用法在之前有讲过,这个 API 就是返回一个函数 + 绑定 this

示例四

let obj = { a: 1 }
function fn(){
    console.log( this )
    console.log( this.a )
}
let newFn = fn.bind( obj )
newFn() 

上述中的 this 会打印出什么呢?

newFn() === fn.bind( obj )() ==> this === obj ==> this.a === 1

示例五

let obj = {
    container: document.querySeletor( 'body' )
    bind: function(){
        console.log( this )
        this.container.addEventListener( 'click', this.sayHello )
        this.container.addEventListener( 'click', this.sayHello.bind( this ) )
    },
    sayHello: function(){
        console.log( this )
    }
}
obj.bind()

上述中的 this 会打印出什么呢?

bind 中的 ` this ` ==> obj.bind.call( obj ) ==> this === obj
this.container.addEventListener( 'click', this.sayHello ) 当点击事件触发时,sayHello 打印出的 this ==> this === 绑定事件的元素(this.container) ==> this === obj.container
this.container.addEventListener( 'click', this.sayHello.bind( this ) ) 当点击事件触发时,sayHello 打印出的 this ==>  bind 绑定了 this[ this === obj ] ==> sayHello 打印出的 this === obj

箭头函数

箭头函数没有 this + arguments

示例六

let obj = {
    fn1: function(){
        console.log( this )
    },
    fn2() {
        console.log( this )
    },
    fn3: () => {
        console.log( this )
    }
}
obj.fn1()
obj.fn2()
obj.fn3()

上述中的 this 会打印出什么呢?

obj.fn1() === obj.fn1.call( obj ) ==> this === obj
obj.fn2() === obj.fn2.call( obj ) ==> this === obj
fn1 的写法 === fn2 的写法
obj.fn3() === obj.fn3.call( 与 obj 同级 this ) ==> this === window

示例七

let obj = {
    init() {
        console.log( this )
        let prop = {
            init: () => {
                console.log( this )
            },
            bind() {
                console.log( this )
            } 
        }
        prop.init()
        prop.bind()
    }
}
obj.init()

上述中的 this 会打印出什么呢?

let obj = {
    init() {
        console.log( this )  // 2.  this === obj
        let prop = {
            init: () => {
                console.log( this )  // 4. this === prop 同级 this ==> this === obj 
            },
            bind() {
                console.log( this )  // 6. this === prop
            } 
        }
        prop.init()  // 3. init() 是箭头函数 ==> prop.init.call( prop 同级 this )
        prop.bind()  // 5. bind 不是箭头函数 ==> prop.bind.call( prop )
    }
}
obj.init()  // 1.  === obj.init.call( obj )

示例八

let obj = {
    x: console.log( this )
    fn1() {
        console.log( this )
        setTimeout( function(){
            console.log( this )
        }, 10 )
    },
    fn2() {
        console.log( this )
        setTimeout( () => {
            console.log( this )
        }, 20 )
    },
    fn3() {
        console.log( this )
        setTimeout( function(){
            console.log( this )
        }.bind( this ), 30 )
    },
    fn4: () => {
        console.log( this )
        setTimeout( () => {
            console.log( this )
        }, 40 )
    }
}
obj.fn1()
obj.fn2()
obj.fn3()
obj.fn4()

obj.x 的值是什么?

上述中的 this 会打印出什么呢?

let obj = {
    x: console.log( this )  // console.log( this ) 的值是 undefined ==> obj.x === undefined  其中的 this === window
}

fn1()

fn1() {
    console.log( this )   // 2. this === obj
    setTimeout( function(){
        console.log( this )  
    }, 10 )
    // 3. 等价于
    // function fn(){
    //     console.log( this )  // 5. this === undefined == 浏览器转化 ==> window
    // }
    // 过 10s 执行函数 fn
    // fn()   // 4. fn 非箭头函数  fn.call( undefined ) 
}
obj.fn1()  // 1. fn1 非箭头函数  obj.fn1.call( obj )

fn2()

fn2() {
    console.log( this )   // 2. this === obj
    setTimeout( () => {
        console.log( this )  // 5. this === 上级 this  ==> this === obj
    }, 20 )
    // 3. 等价于
    // () => {
    //     console.log( this )
    // }
    // 过 20s 执行箭头函数
    // 箭头函数()  // 4. 箭头函数 箭头函数.call( 上级this )
}
obj.fn2()  // 1. fn2 非箭头函数  obj.fn2.call( obj )

fn3()

fn3() {
    console.log( this )  // 2.  this === obj
    setTimeout( function(){
        console.log( this )
    }.bind( this ), 30 )
    // 3.  等价于
    // function fn(){
    //     console.log( this )  // 5. this === obj
    // }.bind( this )
    // 过 30s 执行函数 fn
    // fn.bind(this)  // 4. fn 非箭头函数 + bind 绑定 this[ this === obj ]
}
obj.fn3()  // 1. fn3 非箭头函数  obj.fn3.call( obj )

fn4()

fn4: () => {
    console.log( this )  // 2. this === obj 同级 this  ==> this === window
    setTimeout( () => {
        console.log( this )
    }, 40 )
    // 3. 等价于
    // () => {
    //     console.log( this )  // 5. this === window
    // }
    // 过 40s 后执行箭头函数
    // 箭头函数()   // 4. 箭头函数  箭头函数.call( 上级 this )
}
obj.fn4() // 1. fn4 箭头函数  obj.fn4.call( obj 同级 this )

总结

判断函数中 this 步骤

  1. 查看调用函数的类型
  2. 箭头函数
    • 箭头函数.call( 上级 this )
    • obj.箭头函数.call( obj 同级 this )
  3. 非箭头函数
    • 非箭头函数.call( undefined )
    • obj.非箭头函数.call( obj )

相关文章

  • 总结篇(三) -- 你知道 log 出来的 this 是什么么

    概述 函数中的 this 是一个很重要的知识点,如果能清楚的知道各种场景下函数中 this 所代表的值,那么对于我...

  • 世界上最贵的国画,是个木匠画出来的,卖了9.43亿

    你知道这个世界最贵的国画价值多少么?你知道这个世界最贵的国画是什么人画出来的么?没事喜欢看看艺术品收藏资讯的朋友肯...

  • 几家欢喜几家愁

    你知道我在想什么么?你知道我现在在心里问自己无数个问题,关于什么么?你知道,我到底想要的是什么么?不,没人知道,我...

  • Charles抓包工具

    Update Log:2017.10.31: 更新charles抓不到的情况总结. 一.是什么? Charles是...

  • 走对圈子跟对人文案

    亲爱的小伙伴们,你知道什么是颤音么? 你听过颤音么? 你听过颤音唱歌是什么感觉么? 想要知道从颤音到颤音改变的神奇...

  • 《小白H5成长之路15》正确认识网页中的文本框

    “小白,你知道文本框么?” “知道,就是网页里面输入文字的地方!” “哦,那你知道表单么?” “不知道,表单是什么...

  • 这个女人,欠她太多

    “你知道今天是什么日子么?” “端午?端午不是明天么??” “……” “端午是明天!!!你又忘了我的生日!!!” ...

  • 2018-09-01

    艾灸,艾你所难,暖你所寒 您了解艾灸么?知道艾灸的原料是什么么?知道它有怎样的疗效么? 今天我们就带大家一起来了解...

  • 吃货的爱情

    你知道么? 其实很多零食在刚被生产出来的时候, 根本不知道自己该是什么味道。 他们慢慢的试啊,找啊。 糖水儿冻成了...

  • Spring+SpringMVC+MyBatis+easyUI整

    日常啰嗦 看到标题你可能会问为什么这一篇会谈到代码测试,不是说代码优化么?前两篇主要是讲了程序的输出及Log4j的...

网友评论

      本文标题:总结篇(三) -- 你知道 log 出来的 this 是什么么

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