繁体   English   中英

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

[英]how is the 'this' keyword of javascript is different from 'this' keyword of java?

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,没有解释器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM