最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

how is the 'this' keyword of javascript is different from 'this' keyword of java? - Stack Overfl

programmeradmin2浏览0评论

How is the 'this' keyword of javascript is different from 'this' keyword of java?any practical example will 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.

How is the 'this' keyword of javascript is different from 'this' keyword of java?any practical example will 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.

Share Improve this question asked Dec 23, 2013 at 4:56 user2623213user2623213 2332 gold badges4 silver badges11 bronze badges 7
  • 2 this key word in both case refer the current object. – Subhrajyoti Majumder Commented Dec 23, 2013 at 4:56
  • 1 In javascript, this can also refer to an element triggering an event. – user1890615 Commented Dec 23, 2013 at 4:59
  • 8 Java is not Javascript, so I think comparison you are doing is, between apples and oranges, which doesn't make sense. – kosa Commented Dec 23, 2013 at 4:59
  • possible duplicate of java programming language. (I know that is a "closed" Question, but the first Answer clearly and succinctly answers this one too.) – Stephen C Commented Dec 23, 2013 at 5:01
  • In ECMAScript, this is set either by the call or using bind. It can reference any object and be different for each call. In addition, in strict mode, this can be set to any value by the call, including undefined. – RobG Commented Dec 23, 2013 at 5:02
 |  Show 2 more comments

3 Answers 3

Reset to default 9

In JavaScript this always refers to the “owner” of the function we're executing, or rather, to the object that a function is a method of.

In Java, this refers to the current instance object on which the method is executed.

JavaScript is a bit peculiar when it comes to the keyword "this".

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

In fact, just read the linked article to understand how JavaScript treats the "this" keyword--drink plenty of coffee first.

ECMAScript defines this as a keyword that "evaluates to the value of the ThisBinding of the current execution context" (§11.1.1). The interpreter updates the ThisBinding whenever establishing an execution context.

In Java this refers to the current instance of the method on which it is used. There is a JVM, and no interpreter.

发布评论

评论列表(0)

  1. 暂无评论