在没有“this”的情况下访问JavaScript中对象中的变量
function Peon(number) {
this.number = number;
this.inc = function() {
number = number + 1;
};
return true;
}
var p = new Peon(10);
function returnNumber() {
p.inc();
alert(p.number);
}
<input id="b00" type="button" value="Click" onclick="returnNumber()">
该代码无法按预期工作。有没有一种方法可以让它工作而不必写
this.number=this.number+1;
Here 这是一个微不足道的选择,但在更大的代码中没有 this.* 会让它更具可读性。是否可以?
function Peon(number) {
this.number = number;
this.inc = function() {
number = number + 1;
};
return true;
}
var p = new Peon(10);
function returnNumber() {
p.inc();
alert(p.number);
}
<input id="b00" type="button" value="Click" onclick="returnNumber()">
This code doesn't work as intended. Is there a way to make it work without having to write
this.number=this.number+1;
Here it is a trivial choice, but in bigger codes not having this.* would make it a lot more readable. Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我能看到的唯一可读的方式是:
否则,在你的“更大的代码”假设中,你可以这样做:
The only readable way I can see doing it would be:
Otherwise, in your "bigger codes" postulation, you could do something like this:
是的,您不需要在 JavaScript 中使用“this”。您可以通过闭包而不是“this”访问变量
Yes, you do not need to use 'this' in javascript. You can access variables via closure instead of 'this'
您可以将
number
设置为“私有”,但是您需要一个 getter:您应该阅读 Douglas Crockfords 的“The Good Parts”,了解有关如何使用此模式的更多信息,在 Google 图书。
另外,您不需要从构造函数返回某些内容,您的
return true
是多余的。You can make
number
"private", but then you need a getter:You should read Douglas Crockfords "The Good Parts" for more information on how to use this pattern, there's (limited) a preview available at Google Books.
Also you don't need to return something from the constructor, your
return true
is superfluous.不可以,您必须使用
this
来引用this
对象上的属性。请注意,JavaScript 中的this
与其他一些语言(例如 C 或 Java)中的this
非常不同。更多此处和此处。您的代码所做的是访问传递给
Peon
构造函数的number
参数,而不是您在中创建的this.number
属性构造函数。这就是为什么它不能按预期工作,但也不会失败。没有理由在
Peon
构造函数中定义inc
操作,顺便说一句,并且有一些很好的理由不这样做(通过 < code>Peon 将获得该函数的自己的副本)。因此,您可以这样定义它:或者使用现代 JavaScript 功能 (ES2015+):
No, you have to use
this
to reference properties on thethis
object. Note thatthis
in JavaScript is very different fromthis
in some other languages, like C or Java. More here and here.What your code is doing is accessing the
number
argument that was passed into thePeon
constructor function, rather than thethis.number
property you created in the constructor. Which is why it doesn't work as intended, but doesn't fail, either.There's no reason to define your
inc
operation within thePeon
constructor function, BTW, and some good reasons not to (every individual object created viaPeon
will get its very own copy of that function). So instead, you can define it like this:Or using modern JavaScript features (ES2015+):
不是真的,但这更简洁
实际上,作为旁注,您最好在 Peon 的构造函数之外声明 .inc。你可以用原型来做到这一点。这样,每次创建 Peon 类型的对象时,就不会重建 inc 函数。
或者,您可以使用
p.number++
,而不是使用p.inc()
。这是我能想到避免使用 this 关键字的唯一方法。Not really, but this is a little more concise
Actually, as a side note, you'd be better off declaring .inc outside the constructor of Peon. You could do this with prototype. That way the inc function is not reconstructed each time you create an object of type Peon.
Or instead of using
p.inc()
you could dop.number++
. That's the only way I can think of avoiding the this keyword.