How is the keyword 'this' javascript different from the keyword 'this' java?

How is the keyword 'this' javascript different from the keyword 'this' java? Any practical example would be appreciated.

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

in java:

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

thanks.

+4
source share
3 answers

JavaScript is a little weird when it comes to the keyword "this".

In JavaScript, functions are objects, and the value of "this" depends on how the function is called .

In fact, just read the related article to understand how JavaScript relates to the keyword "this" - drink a lot of coffee first.

+5
source

JavaScript this "" , , , , , .

Java this , .

+2

ECMAScript this , " ThisBinding " (§11.1.1). ThisBinding .

Java this , . JVM .

+1

All Articles