javascript的’this’关键字与java的’this’关键字有什么不同?

javascript的’this’关键字与java的’this’关键字有什么不同?任何实际的例子将不胜感激。

var counter = { val: 0, increment: function () { this.val += 1; } }; counter.increment(); console.log(counter.val); // 1 counter['increment'](); console.log(counter.val); // 2 

在java中:

  public Rectangle(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } 

谢谢。

关于关键字“this”,JavaScript有点奇怪。

在JavaScript中,函数是对象, “this”的值取决于函数的调用方式 。

事实上,只需阅读链接文章,了解JavaScript如何处理“this”关键字 – 首先喝大量咖啡。

JavaScript中, this总是指我们正在执行的函数的“所有者”,或者更确切地说,指向函数是其方法的对象。

Java中this指的是执行该方法的当前实例对象。

ECMAScript this定义为“评估当前执行上下文的ThisBinding的值”的关键字(第11.1.1节)。 每当建立执行上下文时,解释器都会更新ThisBinding。

在Java中, this指的是使用它的方法的当前实例。 有一个JVM,没有解释器。