Re: OOPs!! When is a class not a class? (Re: Number/Specificality/Archetypes in Language)
From: | Keith Gaughan <kmgaughan@...> |
Date: | Thursday, September 23, 2004, 20:53 |
Ray Brown wrote:
> (Yes, I'm replying to myself!)
>
> On Wednesday, September 22, 2004, at 08:23 , Ray Brown wrote:
>
>> On Tuesday, September 21, 2004, at 09:32 , Philippe Caquant wrote:
>
> [snip]
>
>>> Yes, looks like that; yet I was very pleased to
>>> discover that in JavaScript, the properties of an
>>> object belonging to a class can be in contradiction
>>> with the general class properties (the prototype ones,
>>> if I got it right). As I understood it, if you refer
>>> to an object's property, Javascript will first look
>>> for an explicit property at the very object level; if
>>> it doesn't find it there, it will look for it at the
>>> prototype level;
>>
>>
>> Yes, yes - and classes may be sub-classes of others.
>>
>> JavaScript has merely taken these ideas over from Java & they've been
>> around in object-oriented programming (OOP) for a few decades.
>
>
> OOPs!! Someone has informed me that JavaScript does *not* have the formal
> notion of class in the sense that the term is used in OOP and in the way
> that C++ and Java does. I must confess that my object-oriented programming
> has been a little in C++ and quite a lot in Delphi and Java. So far I've
> done _very_ little in JavaScript, so when Philippe mentioned objects and
> classes in JavaScript, I assumed that the terms were being used in the
> traditional OOP manner. I should have known better: other than a
> resemblance in syntax, JavaScript has nothing to do with Java. (Darned
> scripting languages :)
>
> In fact, I discover the way JavaScript deals with objects is quite
> different the classic OOP languages. It seems books on JavaScript are in
> the habit of using the term 'class' informally for, as I understand it, a
> set of objects sharing similar properties & methods. But - {blushes deeply}
> - if I had stopped to think about it, Javascript could not have formal
> classes because it is such a weakly typed language. (Darned scripting
> languages :)
>
> OK - Philippe, if your only experience of using objects is JavaScript,
> maybe we had better not continue using the class ~ object analogy
> otherwise we are very likely to be talking at cross-purposes, which won't
> help anybody.
Oh, for the time! JavaScript uses prototype-based OO, unlike the
class-based OO of most languages. The thing about POP is that it's
far more powerful and flexible than classed-based OO.
I don't have a lot of time to go into it. But I'll say that it's a
really bad idea to try and program JavaScript like a class-based
language. While you can, you end up missing out on a lot of the power
it has hidden within. Off the top of my head, I'd so say read the
following:
* http://en.wikipedia.org/wiki/Prototype-based_programming
* http://en.wikipedia.org/wiki/Closure_%28computer_science%29
* http://en.wikipedia.org/wiki/Function_objects
* http://c2.com/cgi/wiki?JavaScript
* http://c2.com/cgi/wiki?WhatIsClosure
* http://c2.com/cgi/wiki?ScopeAndClosures
* http://c2.com/cgi/wiki?ClosuresAndObjectsAreEquivalent
* http://c2.com/cgi/wiki?LexicalClosure
* http://c2.com/cgi/wiki?ClassesVsPrototypes
* http://c2.com/cgi/wiki?PrototypeBasedLanguage
--
Keith Gaughan -- talideon.com
The man who removes a mountain begins
by carrying away small stones...
...to make place for some really big nukes!
Reply