Javascript中的类变量 [英] Class Variables in Javascript

查看:127
本文介绍了Javascript中的类变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试让类变量在javascript中运行时遇到了一些麻烦。

I'm having a bit of trouble trying to get class variables to work in javascript.

我认为我理解原型继承模型,但显然不是。我假设原型将在对象之间共享,那么它们的变量也是如此。

I thought that I understood the prototype inheritance model, but obviously not. I assumed that since prototypes will be shared between objects then so will their variables.

这就是为什么这段代码让我感到困惑。

This is why this bit of code confuses me.

实现类变量的正确方法是什么?

What is the correct way to implement class variables?

function classA() {};

classA.prototype.shared = 0;

a = new classA;

//print both values to make sure that they are the same
classA.prototype.shared;
a.shared;

//increment class variable
classA.prototype.shared++;

//Verify that they are each 1 (Works)
classA.prototype.shared;
a.shared;

//now increment the other reference
a.shared++;

//Verify that they are each 2 (Doesn't Work)
classA.prototype.shared;
a.shared;

更新:
所以似乎每个人都通过递增实例的变量来确认这一事实我们不会影响原型。这很好,这是我在我的例子中记录的内容,但这不是语言设计中的错误吗?为什么这种行为是可取的?我觉得奇怪的是,当实例的var未定义时,我们遵循原型的隐藏链接,我们得到var的值,但我们将它复制到实例对象中。

UPDATE: So it seems that everyone is confirming the fact that by incrementing the instance's variable we don't affect the prototype. This is fine, this is what I have documented in my example, but doesn't this seem like an error in the design of the language? Why would this behavior be desirable? I find it weird that when the instance's var is undefined we follow the hidden link to the prototype where we get the value of the var, but we copy it into the instance object.

我也明白这不是java / c ++ / ruby​​ / python,它是一种不同的语言。我只是好奇为什么这种行为可能会很好。

I also understand that this isn't java/c++/ruby/python, it's a different language. I'm just curious as to why this behavior might be good.

推荐答案

I assumed that since prototypes will be shared between objects then so will their variables.

他们是,但是这个:

a.shared++

没有做你认为它正在做的事情。它实际上是(大约)糖语法:

is not doing what you think it's doing. It's in fact (approximately) sugar syntax for:

(a.shared= a.shared+1)-1

(-1表示返回预增量值,而不是您实际使用的是retrun值,但仍然。)

(the -1 being to return the pre-increment value, not that you're actually using the retrun value, but still.)

所以这实际上是对a.shared的一个分配。当您分配给实例成员时,您总是写入该实例自己的成员,触及其任何原型的任何成员。它与说法相同:

So this is actually doing an assigment to a.shared. When you assign to an instance member you are always writing to that instance's own members, not touching any members of any of its prototypes. It's the same as saying:

classA.prototype.shared= 1;
a.shared= 2;

所以你的新a.shared会隐藏prototype.shared而不会改变它。 classA的其他实例将继续显示原型的值1.如果删除a.shared,您将再次能够看到隐藏在其后面的原型变量。

So your new a.shared hides the prototype.shared without altering it. Other instances of classA would continue to show the prototype's value 1. If you deleted a.shared you would once again be able to see the prototype's variable that was hidden behind it.

这篇关于Javascript中的类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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