javascript的'this'关键字与java的'this'关键字有什么不同? [英] how is the 'this' keyword of javascript is different from 'this' keyword of java?

查看:77
本文介绍了javascript的'this'关键字与java的'this'关键字有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

javascript的'this'关键字与java的'this'关键字有什么不同?任何实际例子都会受到赞赏。

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

java中的

in 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 is a bit peculiar when it comes to the keyword "this".

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

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

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

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

这篇关于javascript的'this'关键字与java的'this'关键字有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆