13.this指向问题

this

1.函数预编译过程,this指向window

2.全局作用域里面this指向window

3.call/apply可以改变函数运行时this指向

4.obj.func();  func()里面的this指向obj

function test(c){

    //var this = Object.create(test.prototype)或者{ __proto__ : test.prototype}

    var  a = 123;

    function b(){}

}

AO:{

    arguments:[1],

    this:window,

    c:1,

    a:undefined,

    b:function(){}

}

test(1);

new test();   //new的时候,会在函数第一行隐式执行加粗的那一行,那么加粗这一行的this会将原来的指向window的this替换掉


function test(){

console.log(this)   //window

}

test();


consle.log(this)    //window


var obj = {

    a: function(){

            console.log(this)   //obj

    },

    name:'xx'

}

obj.a();


var name = "222";

var a = {

    name : "111",

    say : function(){

        console.info(this.name);

    }

}

var fun = a.say;

fun()

a.say()

var b = {

    name:"333",

    say:function(fun){

        fun();

    }

}

b.say(a.say)

b.say = a.say

b.say()

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容