On Wednesday, October 13, 2010 07:51:19 pm Eric C. wrote:
the language itself because they pretty much always existed as part of
I’m not sure I follow. It sounds like you’re saying that the
expression “o.foo” can mean either “invoke method foo on object o” or
“instance variable foo of object o”, depending on how o is defined.
My point was that JavaScript is like this, while Ruby is not. In
JavaScript,
ordinarily, ‘o.foo’ can only ever mean an instance variable (it has to
be
o.foo() to be a method, and if foo is a method, o.foo just gives you the
function object for that method). However, we can also specify a
function
which will be called whenever someone tries to access ‘o.foo’.
In other words, JavaScript makes it obnoxiously more tedious to either
use
normal setters and getters (for example, o.foo, o.setFoo(), and
o.getFoo()),
and even more tedious to define first-class setters and getters (so
trying to
set or get o.foo actually calls your custom code instead). However, it
makes
it very easy to remix objects, with far more flexibility and with a far
more
primitive concept than mixins.
Ruby, by contrast, makes setters and getters easy (and required), which
means
encapsulation is much easier – but it makes certain kinds of code
re-use
harder, and forces you to use classes and modules, whereas JavaScript
starts
out with something simpler (prototypal inheritance), and allows you to
pretty
much build any inheritance model you like on top of that.
Anyway, this is all about syntax. JavaScript makes it easier to do
things one
way, Ruby makes it easier another way, but everything I mentioned should
be
possible in both. It’s just a question of how ugly it will be.