--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.
~ Anders
"my string".tty.green
On Fri, Jun 11, 2010 at 1:46 PM, Marak Squires <marak....@gmail.com> wrote:
> Not being OO?
> Scared of prototyping String?
> lol you guys, good joke
You can take the comments or leave them, but don't be surprised when
people respond to an email you send to a list.
Polymorphism and encapsulation are the *point* of OOP.
--i
:p the internet is serious bidness!your namespacing suggestion isnt that bad. in my general coding practices the only thing i would allow to prototype String is a helper library. i'm almost never going to be prototyping String in my applications code.with that being said, you do have a point. i think the best compromise here would be to introduce a noconflict mode or something along the lines of that? this way a developer could specify an optional namespace like "tty" if they wanted?
I'm glad we have a really easy example of object prototyping in JavaScript that everyone can understand and apply their own knowledge and experience to. Surely this collaborative discussion will yield a fruitful bounty of information in which we can all benefit from.
JavaScript isn't most OO languages. There are no classes, no
interfaces, etc. It's just objects and functions all the way down.
The purpose of polymorphism – not the implementation or syntax, mind
you – is to abstract out functionality away from where it doesn't
matter, while preserving a consistent interface. Since the interface
remains constant, consumers can just use it, and trust that it will
work appropriately.
The idea is that you'd have some consumer code that does "foo".green,
without knowing exactly what the actual result will be, but trusting
that it will be green-ified in some manner than makes sense.
Then you have a tty-colors module that will attach the
String.prototype.green getter to return a string with the tty escape
codes (and perhaps strips them off if the output is not a TTY) and
let's say an html-colors module that attaches a String.prototype.green
getter that returns a string with html tags.
A fourth module switches between loading the appropriate coloring
module for the current environment. That is the *only* one that knows
whether it's dealing with HTML or TTY output.
So, you can do "foo".green, and it'll work the same, whether it's a
TTY or a web page, without having decision points all over the place
that decide which one to use.
In a C++ or Java world, you might have something like this:
String greenStr = StringColorizerFactory::New()->green( myString );
and then have StringColorizerFactory return an object that implements
the StringColorizerImpl implementation (either a StringColorizerTTY or
StringColorizerHTML).
Too often, people hear OOP and think Classes. OOP was never about
classes, but rather about encapsulation and message passing, since
that makes it *possible* to have consistent interfaces. With
JavaScript, you can just throw all that garbage away, and deal with
the principles of OOP much more directly. That's why I love it, even
with its warts.
Marak didn't give you the HTML outputting functionality, or the thing
to switch between them. But I'd argue that his (well, cloudhead's)
approach for handling the TTY coloring is actually OOP done right.
--i
I'm glad we have a really easy example of object prototyping in JavaScript that everyone can understand and apply their own knowledge and experience to. Surely this collaborative discussion will yield a fruitful bounty of information in which we can all benefit from.
In other words, whichever thing is deciding which set of colors to
attach to the String prototype must know where *all* the output is
going.
> But if I implement html and tty using defineProperty, on the second call, I
> get "Cannot redefine property".
Unless you add { configurable : true } to the property definition.
But the point is valid, globals suck in a lot of use cases.
I'd argue that if you have a case where all of your output is
definitely going to one place, then modifying the global String
prototype could yield complexity benefits. But if you need to have
both available, then yes, either the colorizing functions need to be
namespaced, or you need to pass around formatter objects, or have a
configuration thing somewhere, like your "html" and "tty" functions
that take a callback. When that callback returns, it can set the
styling mechanism back the way it was, so nesting works fine.
--i
As I see it this whole discussion is moot.
--
Tom -I'm sure there are use cases but I really dislike that approach.