繁体   English   中英

可以用“this”关键字声明变量吗?

[英]can variables be declared with the "this" keyword?

例如,例如this.x=5并且如果可以声明它们都可以访问的位置。

<script>
$(document).ready(function(){
  $("button").click(function(){
  this.x=!this.x;
    $("#div1").fadeTo(400,this.x ? 0.4 : 1);
  });
});
</script>

我们可以使用 this 关键字定义一个变量,这里变量的范围因我们定义变量的位置而异。 例如,我们可以这样定义 this.x=10; 现在是 console.log(x) //10; this.x === this.x \\\\true this.x == this.x \\\\true this.x != this.x \\\\false

这不是在 javascript 中声明变量的正确方法。 但是,它会在某些情况下起作用,将您的变量附加到当前范围。

暂无
暂无

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

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