Forums » Discussions »
Scoping in objects
Added by Heinz Gies over 1 year ago
Let's discus how to handle scoping in objects. Curently every object posesses it's own environment and by that it's own storage for functions and variables. So I think it is possible to make them static too.
Replies
RE: Scoping in objects
-
Added by Kornelius Kalnbach over 1 year ago
I don't think you can make object member/method invocation static in a dynamically typed language due to polymorphism and open classes/objects.
Actually, I think scope (local vs. global, inner vs. outer variables and functions) is a totally different thing from object namespace.
This part is completely confusing :( sorry.
In JavaScript you only get access to an objects namespace by explicitly stating the receiver. You even have to use this inside of the object's method themselves. The only exception is the global object (top level this) which stores global variables as members. It's a confusing strangeness.
In Ruby, everything that you see from an object on the outside are the instance methods of the object's metaclass, class, and the class's superclasses. Members (instance variables) are only seen from inside the object. Since there are no functions, every method is called with an explicit or implicit receiver (self). Which method gets called depends on the receiver's classes, and can even be method_missing. Constants work about the same. Variable scope, on the other hand, is lexical (and thus, static).
We're actually talking about four different things:
| Language | Variable | Function | object.foo |
Member/Instance variable | Comment |
|---|---|---|---|---|---|
| Ruby | lexical scope | - | methods only | via @ivar |
Objects store instance variables, classes store methods, classes are objects. |
| JavaScript | lexical scope | - | members | via this |
Objects are containers, method calls are really just [receiver. or global object]function-object(). |