美文网首页
javascript.this

javascript.this

作者: 359c7a79d70f | 来源:发表于2016-07-10 22:57 被阅读7次
    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
    */
    

    相关文章

      网友评论

          本文标题:javascript.this

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