【问题标题】:what is 'this' is javascript什么是 'this' 是 javascript
【发布时间】:2010-12-31 03:31:00
【问题描述】:

我能问一下javascript中的'this'是什么意思吗,'this'上的任何资源,以及this.submit在表单中的含义是什么?

【问题讨论】:

  • 你的意思是关于“这个”的任何资源吗?
  • 你在发帖前调查过这个问题吗?
  • this.submit() 在表单的上下文中会提交表单...如果您考虑一下,这是有道理的。

标签: javascript this


【解决方案1】:

【讨论】:

  • mozilla 有很多 javascript 教程?能给我一个链接吗?mozilla javascript 教程合集在哪里?
【解决方案2】:

【讨论】:

    【解决方案3】:

    this 指的是当前对象。所以如果我有类似的东西:

    var someVar = {
      some: 'thing',
      another: function(){
        return "thing";
      },
      more: function(){
        this.some + this.another();
      }
    }
    

    那么这些语句中的this 指的是someVar,所以你调用的函数another()someVar 的成员...

    【讨论】:

      【解决方案4】:

      this 是对对象的 self 引用。

      在对象中使用,this 指的是该对象本身。

      在外部(不在对象内)使用this 指的是全局 对象。

      这样

        function Func(z) { this.x = z; }
      

      正在运行

        Func(7);
      

      全局变量x设置为7。

      在做的时候

        var o = new Func(3);
        var p = new Func(4);
      

      会创建两个对象op,并将Func实例化x属性设置为o 为 3,p 为 4。

      this site 上有精心设计的解释。

      【讨论】:

        【解决方案5】:

        下面 PDF 中的文本深入解释了“这个” - 以及它是如何变化的。

        https://github.com/spencertipping/js-in-ten-minutes/blob/master/js-in-ten-minutes.pdf

        (对不起,网上不能直接访问,你必须下载东西)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-20
          • 2015-08-10
          • 1970-01-01
          • 2011-11-06
          • 2013-05-05
          • 1970-01-01
          相关资源
          最近更新 更多