Scott Sauyet
4/5/2016 4:04:00 AM
John Harris wrote:
> Scott Sauyet wrote:
>>John Harris wrote:
> <snip>
>>> The three examples illustrate :
>>> 1 Class-style creation : Using an ES6 class declaration;
>>> 2 Class-style creation : Using ES3 features;
>>> 3 Prototypal-style creation : Using ES3 features.
>>
>> These three also illustrate standard prototypal creation, regardless of
>> whether some syntactic sugar has been added to help make them seem more
>> similar to C++-style OOP classes.
> <snip>
>
> I'm afraid I disagree with you there.
>
> First, in a full-on prototypal language a new object is a complete copy
> of the prototype. [ ... ]
I don't feel like looking up where, but I believe we've had this
discussion before. I do not believe that concatenative prototyping is
the only legitimate form of prototypal inheritance. Delegation-based
prototyping is reasonable, and has been part of prototypal languages
since the very beginning. Self had a delegation mechanism.
> Second, let's look at a typical C++ implementation. Each object contains
> a pointer to a structure shared with all objects belonging to the same
> class. The shared structure contains pointers to the class's method
> functions. On seeing
> a.oink();
> the compiler generates code that follows the pointer to the shared
> structure, picks out the pointer it knows points to the oink function,
> follows that pointer and calls the oink function. One of the call
> arguments is a pointer to the object (i.e 'this') so the method knows
> what to work on. The 'this' argument is automatic, not defined in the
> function definition.
>
> Now look at ECMAScript. Each object contains a pointer to a structure
> shared with all objects of the same kind (or null - programmer's
> choice). The structure holds pointers to method functions (or not -
> programmer's choice). Each method call automatically includes a 'this'
> argument, held in a location separate from the user's arguments.
You've made this point here a number of times.
I still disagree.
There are certainly significant implementation similarities between
delegation-based prototypes and classes, but there are also significant
differences:
obj.prototype.method(...params);
What's the equivalent of that in a class-based language? In general,
there is none, because _classes are not objects_. Prototypes are
objects. While the above code may fail because the implementation of
`method` might depend upon some properties available only on instances
one level down, it also might work. And more importantly, there is
simply nothing like this in C++ or Java or similar languages, for the
very good reason that the class of an object is a different sort of beast
altogether from the object itself. In Javascript, they are both objects,
and overlap in capabilities as well.
> The two languages are remarkably similar. By your definition C++ is a
> prototypal language with class declarations bolted on for programmers'
> convenience.
Sorry, that's your straw-man, and not my definition.
> ECMAScript is a very flexible language. Most things can be done in many
> different ways. My view is that it is flexible enough to emulate both
> styles of language, but is neither in full.
I'm not sure what you mean by "both styles" here. There are so many
different ways to do OOP that choosing one can become a serious chore.
But I do mostly FP these days, and don't worry about it too much
regardless.
> PS Your web site goes bang on the 'photo gallery' link.
Thanks. Some day, I'll take down that 14 year old page. (Those kids are
now 16 and 19 years old!) But it never seems to be important enough to
put up a new one.