javascript.this

2016-07-10  本文已影响7人  359c7a79d70f
var strict = (fuction(){return !this; }());

this 是一个关键字不是变量,也不是属性名;js不允许给this赋值。
关键字this 没有作用域的限制;嵌套函数不会从调用它的函数中继承this!
如果嵌套函数作为方法调用,this的值指向调用它的对象
如果嵌套函数作为函数调用,其this值不是全局对象就是undefined(use strict;)
例子:

var o ={
    m:function(){
        var self = this;
        console.log(self === o);
        f();
        function f(){
            console.log(this === o);
            console.log(self === o);
        }
    }
}
o.m();
/**
    true
    false
    true
*/
上一篇 下一篇

猜你喜欢

热点阅读