this指向

ES5中this指向:

this的指向为:被调用的对象(谁调用我,我就指向谁);

var foo=function(){
    this.a="a",
        this.b="b",
        this.c={
           a:"a+",
           b:function(){
            return this.a;
        }
    }

}

console.log(new foo().c.b());   //a+

 因为是c指向的b,所以this指的是c

 

ES6中箭头函数的指向:

是指定义时函数的指向:

var foo=function(){
        this.a="a",//指向的是foo函数的实例
        this.b="b",
        this.c={
           a:"a+",
           b:()=>{
            return this.a;//this指向的是函数的实例foo
        }
    }

}

console.log(new foo().c.b());   //a

 定义时的值是多少就是多少

 

posted @ 2018-04-25 16:24  梅梅姐  阅读(130)  评论(0编辑  收藏  举报