Is it true that in most Object Oriented Programming Languages, an "i" in an instance method always r
Posted
by Jian Lin
on Stack Overflow
See other posts from Stack Overflow
or by Jian Lin
Published on 2010-05-06T08:36:58Z
Indexed on
2010/05/06
8:48 UTC
Read the original article
Hit count: 207
In the following code:
<script type="text/javascript">
var i = 10;
function Circle(radius) {
this.r = radius;
this.i = radius;
}
Circle.i = 123;
Circle.prototype.area = function() { alert(i); }
var c = new Circle(1);
var a = c.area();
</script>
What is being alerted? The answer is at the end of this question.
I found that the i
in the alert call either refers to any local (if any), or the global variable. There is no way that it can be the instance variable or the class variable even when there is no local and no global defined. To refer to the instance variable i
, we need this.i
, and to the class variable i
, we need Circle.i
. Is this actually true for almost all Object oriented programming languages? Any exception? Are there cases that when there is no local and no global, it will look up the instance variable and then the class variable scope? (or in this case, are those called scope?)
the answer is: 10 is being alerted.
© Stack Overflow or respective owner