Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Overriding function and adding a bit

66 views
Skip to first unread message

Andrew Poulos

unread,
May 9, 2017, 9:59:58 PM5/9/17
to
I'm working with a tool that generates the HTML etc for an elearning
course. In The JavaScript that it generates there's this function


function ObjLayerMoveTo(x, y) {
/* 100+ lines of spaghetti code */
}


This is fine except there are a number of times when the function is
called on an object when I don't want the function to run.

Basically I want it to be something like


function ObjLayerMoveTo(x, y) {
if (this.id == "foo") return;

/* 100+ lines of spaghetti code */
}


To override the existing function and be able to add my code at the top
I was thinking something like this might work:


var myObjLayerMoveTo = window.ObjLayerMoveTo;

window.ObjLayerMoveTo = function(x, y) {
if (this.id == "foo") return;
myObjLayerMoveTo(x, y);
};


Is this the way to approach this?

Andrew Poulos

Doc O'Leary

unread,
May 10, 2017, 11:36:59 AM5/10/17
to
For your reference, records indicate that
Andrew Poulos <ap_...@hotmail.com> wrote:

> var myObjLayerMoveTo = window.ObjLayerMoveTo;
>
> window.ObjLayerMoveTo = function(x, y) {
> if (this.id == "foo") return;
> myObjLayerMoveTo(x, y);
> };
>
>
> Is this the way to approach this?

Goodness, no. A major point of OOD is that you can override the
method in the hierarchy at the appropriate object level. Define
an empty ObjLayerMoveTo function on your “foo” object.

--
"Also . . . I can kill you with my brain."
River Tam, Trash, Firefly


Thomas 'PointedEars' Lahn

unread,
May 10, 2017, 1:13:19 PM5/10/17
to
Andrew Poulos wrote:

> function ObjLayerMoveTo(x, y) {
> /* 100+ lines of spaghetti code */
> }
>
> […]
> var myObjLayerMoveTo = window.ObjLayerMoveTo;
>
> window.ObjLayerMoveTo = function(x, y) {
> if (this.id == "foo") return;
> myObjLayerMoveTo(x, y);
> };
>
>
> Is this the way to approach this?

No, it should be

var myObjLayerMoveTo = ObjLayerMoveTo;

ObjLayerMoveTo = function (x, y) {
if (this.id == "foo") return;
myObjLayerMoveTo(x, y);
};

Do not confuse the “window” object with the ECMAScript global object.

--
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.

Thomas 'PointedEars' Lahn

unread,
May 10, 2017, 1:14:43 PM5/10/17
to
Doc O'Leary wrote:
^^^^^^^^^^^
Get a real name.

> Andrew Poulos <ap_...@hotmail.com> wrote:
>> var myObjLayerMoveTo = window.ObjLayerMoveTo;
>>
>> window.ObjLayerMoveTo = function(x, y) {
>> if (this.id == "foo") return;
>> myObjLayerMoveTo(x, y);
>> };
>>
>>
>> Is this the way to approach this?
>
> Goodness, no. A major point of OOD is that you can override the
> method in the hierarchy at the appropriate object level.

Pray tell, what would be the object here?

> Define an empty ObjLayerMoveTo function on your “foo” object.

Who said anything about a “‘foo’ object”? He wants to *override the
function*.

Thomas 'PointedEars' Lahn

unread,
May 10, 2017, 1:22:44 PM5/10/17
to
Thomas 'PointedEars' Lahn wrote:

> Andrew Poulos wrote:
>> function ObjLayerMoveTo(x, y) {
>> /* 100+ lines of spaghetti code */
>> }
>>
>> […]
>> var myObjLayerMoveTo = window.ObjLayerMoveTo;
>>
>> window.ObjLayerMoveTo = function(x, y) {
>> if (this.id == "foo") return;
>> myObjLayerMoveTo(x, y);
>> };
>>
>>
>> Is this the way to approach this?
>
> No, it should be
>
> var myObjLayerMoveTo = ObjLayerMoveTo;
>
> ObjLayerMoveTo = function (x, y) {
> if (this.id == "foo") return;
> myObjLayerMoveTo(x, y);
> };
>
> Do not confuse the “window” object with the ECMAScript global object.

Or, to keep the global namespace clean:

/* in global context */
(function (global) {
var _ObjLayerMoveTo = global.ObjLayerMoveTo;

global.ObjLayerMoveTo = function (x, y) {
if (this.id == "foo") return;
_ObjLayerMoveTo(x, y);
};
}(this));

or

/* in global context */
ObjLayerMoveTo = (function (global) {
var _ObjLayerMoveTo = global.ObjLayerMoveTo;

return function (x, y) {
if (this.id == "foo") return;
_ObjLayerMoveTo(x, y);
};
}(this));

I recommend either “_ObjLayerMoveTo” or “theirObjLayerMoveTo”, because
“myObjectLayerMoveTo” would semantically be the function with which you
override *their* “ObjLayerMoveTo”.

Doc O'Leary

unread,
May 10, 2017, 7:00:44 PM5/10/17
to
For your reference, records indicate that
Thomas 'PointedEars' Lahn <Point...@web.de> wrote:

> Doc O'Leary wrote:
> ^^^^^^^^^^^
> Get a real name.

Attempt at humor, or trolling from the get-go?

> > Andrew Poulos <ap_...@hotmail.com> wrote:
> >> var myObjLayerMoveTo = window.ObjLayerMoveTo;
> >>
> >> window.ObjLayerMoveTo = function(x, y) {
> >> if (this.id == "foo") return;
> >> myObjLayerMoveTo(x, y);
> >> };
> >>
> >>
> >> Is this the way to approach this?
> >
> > Goodness, no. A major point of OOD is that you can override the
> > method in the hierarchy at the appropriate object level.
>
> Pray tell, what would be the object here?

Whatever the context “this” is referring to such that the id can be
compared in the first place.

> > Define an empty ObjLayerMoveTo function on your “foo” object.
>
> Who said anything about a “‘foo’ object”? He wants to *override the
> function*.

If you don’t see the use of “foo” in the example code, I cannot help
you.

Thomas 'PointedEars' Lahn

unread,
May 10, 2017, 7:49:41 PM5/10/17
to
Doc O'Leary wrote:

> Thomas 'PointedEars' Lahn <Point...@web.de> wrote:
>> Doc O'Leary wrote:
>> ^^^^^^^^^^^
>> Get a real name.
>
> Attempt at humor, or trolling from the get-go?

Are the subscribers of this newsgroup supposed to believe that “Doc O’Leary”
is your real name?

>> > Andrew Poulos <ap_...@hotmail.com> wrote:
>> >> var myObjLayerMoveTo = window.ObjLayerMoveTo;
>> >>
>> >> window.ObjLayerMoveTo = function(x, y) {
>> >> if (this.id == "foo") return;
>> >> myObjLayerMoveTo(x, y);
>> >> };
>> >>
>> >> Is this the way to approach this?
>> > Goodness, no. A major point of OOD is that you can override the
>> > method in the hierarchy at the appropriate object level.
>> Pray tell, what would be the object here?
>
> Whatever the context “this” is referring to such that the id can be
> compared in the first place.

“this” is not necessarily referring to the same object that the function is
called as a method of.

The original code of the function is:

function ObjLayerMoveTo(x, y) {
/* 100+ lines of spaghetti code */
}

>> > Define an empty ObjLayerMoveTo function on your “foo” object.
>> Who said anything about a “‘foo’ object”? He wants to *override the
>> function*.
>
> If you don’t see the use of “foo” in the example code, I cannot help
> you.

"foo" is a primitive string value there.

Thomas 'PointedEars' Lahn

unread,
May 11, 2017, 11:03:17 AM5/11/17
to
Thomas 'PointedEars' Lahn wrote:

> Doc O'Leary wrote:
>> Thomas 'PointedEars' Lahn <Point...@web.de> wrote:
>>> > Andrew Poulos <ap_...@hotmail.com> wrote:
>>> >> var myObjLayerMoveTo = window.ObjLayerMoveTo;
>>> >>
>>> >> window.ObjLayerMoveTo = function(x, y) {
>>> >> if (this.id == "foo") return;
>>> >> myObjLayerMoveTo(x, y);
>>> >> };
>>> >>
>>> >> Is this the way to approach this?
>>> > Goodness, no. A major point of OOD is that you can override the
>>> > method in the hierarchy at the appropriate object level.
>>> Pray tell, what would be the object here?
>>
>> Whatever the context “this” is referring to such that the id can be
^^^^^^^^^^^^^^^^^^
>> compared in the first place.
>
> “this” is not necessarily referring to the same object that the function
> is called as a method of.

Also, the statement above speaks of the common misconception that “this” in
ECMAScript implementations referred to a(n execution) context. It does
_not_.

“this” in a function (execution) context refers to the object of which the
function is called as a method, or its value is the “undefined” value.

In normal mode, a *direct* function call is equivalent to calling the
function explicitly as a method of the global object (unless a execution
context before the global context declares a method of the same name).
That is often *misunderstood* as “this” referring to a(n execution) context
(instead of an object), because symbols declared in the global context are
also available as properties of the global object. In strict mode, however,
“this” is undefined in that case.

This – and, literally, “this” – can be implicit, using property accessor
syntax (dot or brackets), for example:

obj.objLayerMoveTo = ObjLayerMoveTo;

obj.objLayerMoveTo(42, 23);

obj["objLayerMoveTo"](42, 23);

This way, “this” inside the called “objLayerMoveTo” *method* would refer to
the same object as “obj”.

But the required first statement is usually unwise with a host object
referred to by “obj” as it would attempt to augment that host object or
override one of its built-in methods.

“this” can also be explicit, using e.g. Function.prototype.call() or
Function.prototype.apply() (there are other possibilities when used as a
callback for e.g. Array prototype methods):

ObjLayerMoveTo.call(obj, 42, 23);

ObjLayerMoveTo.apply(obj, [42, 23]);

That is safe with a host object referred to by “obj” as it does not augment
it, but still unwise here because of the global function (it “pollutes the
global namespace”).

One can surmise from the name of the function and the use of an “id”
property in the one that is to “override” it, that “this” is supposed to
refer to an element object, which is a host object. But while it is
possible to some extent to modify the prototype of host objects – e.g.,
suppose “obj” would refer to a “div” element object,

HTMLDivElement.prototype.objLayerMoveTo = function (x, y) {
/* … */
};

– it is not compatible, and that modification would not have an effect if
the host object had been augmented. Further, modifying the method that the
host object had been augmented with would compound this former mistake with
the same one.

However, it should be noted that if something like

obj.objLayerMoveTo = ObjLayerMoveTo;

or

obj.onmouseover = ObjLayerMoveTo;

had been executed *before*, then, *without* changing more code, it would be
necessary to do that anyway. [The latter assignment would also be _safe_
and _not_ a mistake by contrast because the values of those (event-handler)
properties of element objects are *designed* to be modified that way.]

Because the suggested redefinition of the function would not change the
reference value that had been assigned to the property of the object
referred to by “obj”:

obj
:
v
,-----------------------.
| : object[Element] |
|-----------------------|
| … |
| +… ----------------------.
| … | :
`-----------------------' : (refers to)
:
,------------------------'
:
: ,-----------------------------------.
`-->| : function[Function] |
|-----------------------------------|
| … |
| +name : string = "ObjLayerMoveTo" |
| … |
`-----------------------------------'
^
. calls
.
,-----------------------------------.
,-->| : function[Function] |
: |-----------------------------------|
: | … |
: | +name : string = "ObjLayerMoveTo" |
: | … |
: `-----------------------------------'
:
`--------------------------.
:
,-------------------------. :
| global : object[Object] | :
|-------------------------| :
| … | :
| +ObjLayerMoveTo -----------'
| … |
`-------------------------'

This is the only way where I think that the *generally* sound idea to
“override the method in the hierarchy at the appropriate object level”
would hold *some* water in *this* case.

Not so if something like

ObjLayerMoveTo.call(obj, 42, 23);

or

ObjLayerMoveTo.apply(obj, [42, 23]);

had been used instead, as “ObjLayerMoveTo” would only be resolved when
execution reached those lines (closure).

John G Harris

unread,
May 11, 2017, 11:33:16 AM5/11/17
to
On Thu, 11 May 2017 01:49:35 +0200, Thomas 'PointedEars' Lahn
<Point...@web.de> wrote:

>Doc O'Leary wrote:
>
>> Thomas 'PointedEars' Lahn <Point...@web.de> wrote:
>>> Doc O'Leary wrote:
>>> ^^^^^^^^^^^
>>> Get a real name.
>>
>> Attempt at humor, or trolling from the get-go?
>
>Are the subscribers of this newsgroup supposed to believe that “Doc O’Leary”
>is your real name?
<snip>

Since when has anyone "subscribed" to a news group ?

Will Thomas ever tell us what a "real" name is ?

Are Thomas 'PointedEars' Lahn, Elizabeth R, Mark Twain, and George
Orwell "real" names ?

Remember what Thomas Lahn said
> But this is a technical newsgroup; we must aspire here to be
> *correct* and *precise* instead of catering to the misconceptions
> and ambiguity of the general public.

John

Julio Di Egidio

unread,
May 11, 2017, 12:56:44 PM5/11/17
to
On Wednesday, May 10, 2017 at 3:59:58 AM UTC+2, Andrew Poulos wrote:
<snip>
> To override the existing function and be able to add my code at the top
> I was thinking something like this might work:
>
> var myObjLayerMoveTo = window.ObjLayerMoveTo;
>
> window.ObjLayerMoveTo = function(x, y) {
> if (this.id == "foo") return;
> myObjLayerMoveTo(x, y);
> };
>
> Is this the way to approach this?

Yes, that's fine, it is in fact the customary solution.

Julio

Julio Di Egidio

unread,
May 11, 2017, 1:05:55 PM5/11/17
to
On Wednesday, May 10, 2017 at 5:36:59 PM UTC+2, Doc O'Leary wrote:
> Andrew Poulos <ap_...@hotmail.com> wrote:
>
> > var myObjLayerMoveTo = window.ObjLayerMoveTo;
> >
> > window.ObjLayerMoveTo = function(x, y) {
> > if (this.id == "foo") return;
> > myObjLayerMoveTo(x, y);
> > };
> >
> > Is this the way to approach this?
>
> Goodness, no. A major point of OOD is that you can override the
> method in the hierarchy at the appropriate object level.

A major point of OO in general is not to overuse/abuse it. That said,
your approach requires changing the underlying implementation, which
sometimes is simply not possible. Indeed the OP was just asking about
how to wrap a method.

Now, back to OO, the good of JavaScript is that you can easily and naturally
do that kind of stuff and more, while you just cannot do anything like that
in a purely OO language...

Julio

Julio Di Egidio

unread,
May 11, 2017, 1:08:13 PM5/11/17
to
On Wednesday, May 10, 2017 at 7:13:19 PM UTC+2, Thomas 'PointedEars' Lahn

> No, it should be
>
> var myObjLayerMoveTo = ObjLayerMoveTo;

That will not work in strict mode...

Julio

Doc O'Leary

unread,
May 11, 2017, 6:06:06 PM5/11/17
to
For your reference, records indicate that
Thomas 'PointedEars' Lahn <Point...@web.de> wrote:

> Are the subscribers of this newsgroup supposed to believe that “Doc O’Leary”
> is your real name?

I don’t have a “real” name. They’ve all been made up, including the one
that was put on my birth certificate. I’ve been using Doc O’Leary on
Usenet since at least 1990:

<https://groups.google.com/forum/?hl=en#!msg/comp.sys.mac.games/-IovAUnRamE/PVW8_4orEo8J>

How about yourself, Pointy? Though I really don’t see how any of this
matters to the topic at hand. Focus.

> “this” is not necessarily referring to the same object that the function is
> called as a method of.

Well, it has to be referring to *something* for the code to work as
expected. I see no reason *not* to call it as a method on the target
object (or prototype thereof), thus allowing the OP to override the
special case of the “foo” object that needs to behave differently.

> The original code of the function is:
>
> function ObjLayerMoveTo(x, y) {
> /* 100+ lines of spaghetti code */
> }

Which says nothing about how it is called, or the larger context in
which it was defined. The issue at hand remains what “this” is
actually referring to. Focus.

> "foo" is a primitive string value there.

Used with a particular intent. My suggestion stands.

Andrew Poulos

unread,
May 11, 2017, 6:32:40 PM5/11/17
to
Beautiful explanation. Thank you.

Andrew Poulos

Gene Wirchenko

unread,
May 11, 2017, 6:40:01 PM5/11/17
to
On Thu, 11 May 2017 01:49:35 +0200, Thomas 'PointedEars' Lahn
<Point...@web.de> wrote:

>Doc O'Leary wrote:
>
>> Thomas 'PointedEars' Lahn <Point...@web.de> wrote:
>>> Doc O'Leary wrote:
>>> ^^^^^^^^^^^
>>> Get a real name.
>>
>> Attempt at humor, or trolling from the get-go?
>
>Are the subscribers of this newsgroup supposed to believe that “Doc O’Leary”
>is your real name?

Who cares (besides you)? As long as Doc O'Leary is consistent in
his name use and the name is not in bad taste, I do not care. Aliases
are perfectly legal provided there is no fraudulent intent.

I knew someone who went by the name of "Doc". He even named his
store "Doc's Computers".

Why don't you complain about my name? My first given name is not
actually "Gene"; it is "Eugene". And I have omitted my middle name,
too. Then, there is the issue of my Chinese name.

[snip]

Sincerely,

Gene Wirchenko

Thomas 'PointedEars' Lahn

unread,
May 11, 2017, 7:53:36 PM5/11/17
to
Doc O'Leary wrote:

> For your reference, records indicate that
> Thomas 'PointedEars' Lahn <Point...@web.de> wrote:

It is called attribution _line_, not attribution novel.

>> Are the subscribers of this newsgroup supposed to believe that “Doc
>> O’Leary” is your real name?
>
> I don’t have a “real” name.

Thanks for the clarification. Score adjusted.

> They’ve all been made up, including the one that was put on my birth
> certificate.

The name on whatever current official document of identity you have is your
real name. Look it up. (But, of course, you already know that.)

> I’ve been using Doc O’Leary on Usenet since at least 1990:

Your problem.

> […]
> How about yourself, Pointy? Though I really don’t see how any of this
> matters to the topic at hand. Focus.

As you can read everywhere, my real name is Thomas Lahn. As customary in
Usenet, I have inserted my nickname between my first name and last name in
the “From” header field value – the former is “PointedEars”, _not_ “Pointy”.
But, of course, as you have been using Usenet “since at least 1990”, you
know all of that. You are just trolling – typical for a pseudonymous
wannabe.

>> “this” is not necessarily referring to the same object that the function
>> is called as a method of.
>
> Well, it has to be referring to *something* for the code to work as
> expected. I see no reason *not* to call it as a method on the target
> object (or prototype thereof), thus allowing the OP to override the
> special case of the “foo” object that needs to behave differently.

My point is that “this” can be anything (except an execution context,
contrary to what you thought), and the comparison would work with
anything except a value for “this” that cannot be converted to an object.

>> The original code of the function is:
>>
>> function ObjLayerMoveTo(x, y) {
>> /* 100+ lines of spaghetti code */
>> }
>
> Which says nothing about how it is called, or the larger context in
> which it was defined.

Exactly. So it is a jump to conclusions on your part that it would be
inappropriate to simply redefine the function as the OP suggested. You
have not even come up with a convincing argument for a case where this
would not work as intended (you left that part to me).

EOD

--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>

Thomas 'PointedEars' Lahn

unread,
May 11, 2017, 9:13:29 PM5/11/17
to
Thomas 'PointedEars' Lahn wrote:

> ,-------------------------. :
> | global : object[Object] | :

This should be

> | : object[global] |

at least for V8 in Node.js.

However, in Chromium the global object appears to have "Window" as the value
of its @@toStringTag internal property.

Since ECMAScript 2015, the concept of the @@toStringTag property of an
object replaces the concept of the internal [[Class]] property for object
types that are not explicitly recognized by the changed
Object.prototype.toString() algorithm.

For objects referred by “obj” that have properties and those whose type is
explictly recognized by the algorithm, obj[Symbol.toStringTag] is the
“undefined” value. So obj[Symbol.toStringTag] could provide a more
efficient to retrieve the equivalent of the former internal [[Class]]
property than parsing the return value of ({}).toString.call(obj), provided
that the @@toStringTag property value has not been modified.

However, the Specification explicitly mentions the possibility that programs
can modify the @@toStringTag property value which makes “legacy type tests”
unreliable.

Example:

var o = {
label: "crook",
exec: function () {
/* there’s no there there */
return null;
}
};

/* I’m not a crook^W^W^W a RegExp, _believe me_[tm]! */
o[Symbol.toStringTag] = "RegExp";

/* [object RegExp] */
({}).toString.call(o);

Such lies can be thwarted:

function dontLieToMe (obj)
{
var desc = Object.getOwnPropertyDescriptor(o, Symbol.toStringTag);

return Object.defineProperty(o, Symbol.toStringTag, {
/* “writable: false, configurable: false” is implicit */
value: desc ? desc.value : void 0
});
}

var o = {
label: "crook",
exec: function () {
/* there’s no there there */
return null;
}
};

dontLieToMe(o);

/* I’m not a crook^W^W^W a RegExp, _believe me_[tm]! */
o[Symbol.toStringTag] = "RegExp";

/* [object Object] */
({}).toString.call(o);

/* “Uncaught TypeError: Cannot redefine property:
Symbol(Symbol.toStringTag)” :-b */
Object.defineProperty(o, Symbol.toStringTag, {
value: desc ? desc.value : void 0,
writable: true
});

But in the end, duck typing may always be the best approach ;-)

<http://www.ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring>
<http://www.ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring>

John G Harris

unread,
May 12, 2017, 5:05:38 AM5/12/17
to
On Fri, 12 May 2017 01:53:30 +0200, Thomas 'PointedEars' Lahn
<Point...@web.de> wrote:

<snip>
>The name on whatever current official document of identity you have is your
>real name. Look it up. (But, of course, you already know that.)
<snip>

What nonsense. If you look up "real name" in the Oxford Dictionary of
English or the Oxford Dictionary of Law you won't find it. What you
will find if you look up "the" in the Oxford Dictionary of English is
that "the O'Donoghue" is a perfectly valid name in the United Kingdom
and Ireland.

As for "official document of identity", some people don't have one. At
least a few years ago French people who gave their children Breton
names were not allowed to have a birth certificate.

Finally, it takes about one minute to find Doc O'Leary's current
employment name, if anyone is in the least bit interested.

John

Thomas 'PointedEars' Lahn

unread,
May 12, 2017, 4:12:26 PM5/12/17
to
Andrew Poulos wrote:

> On 12/05/2017 1:03 AM, Thomas 'PointedEars' Lahn wrote:
>> […]
>
> Beautiful explanation. Thank you.

You are welcome.

Doc O'Leary

unread,
May 12, 2017, 5:46:50 PM5/12/17
to
For your reference, records indicate that
Thomas 'PointedEars' Lahn <Point...@web.de> wrote:

> Doc O'Leary wrote:
>
> > For your reference, records indicate that
> > Thomas 'PointedEars' Lahn <Point...@web.de> wrote:
>
> It is called attribution _line_, not attribution novel.

Oh, boo hoo again. Please consult with a mental health professional
if these sorts of things are really causing you distress.

> > I’ve been using Doc O’Leary on Usenet since at least 1990:
>
> Your problem.

Hasn’t been. It really seems like you’re the only one who has
trouble with variable names. Get it? Ha-Haaaaa!

> > How about yourself, Pointy? Though I really don’t see how any of this
> > matters to the topic at hand. Focus.
>
> As you can read everywhere, my real name is Thomas Lahn.

Nobody would care one way or the other if it wasn’t. This is all
entirely off topic anyway, which gets back to your inability to
focus on what’s really important. Again, seek professional help
on that.

> You
> have not even come up with a convincing argument for a case where this
> would not work as intended (you left that part to me).

It is pointless to make any argument in either direction. It is
simply incumbent on the OP to either clarify his code so that he
can be given a more specific answer, or to take my suggestion to
just use polymorphism, which exists precisely to address this sort
of need. Everything else is idle speculation.

Julio Di Egidio

unread,
May 12, 2017, 8:28:08 PM5/12/17
to
On Friday, May 12, 2017 at 11:46:50 PM UTC+2, Doc O'Leary wrote:
<snip>
> It is
> simply incumbent on the OP to either clarify his code so that he
> can be given a more specific answer, or to take my suggestion to
> just use polymorphism, which exists precisely to address this sort
> of need. Everything else is idle speculation.

Your "this sort of need" is the idle speculation here: indeed your
suggestion remains just wrong, i.e. regardless of the OP. I'll say it
more explicitly: to suggest OO patterns to architect JS code is simple
an utter mistake, that you propose so or that some vendors now are
proposing so...

That said, please do get a real name and email address: it is basic Usenet
etiquette, indeed since the old days.

*Plonk*

Julio

John G Harris

unread,
May 13, 2017, 9:39:44 AM5/13/17
to
On Fri, 12 May 2017 17:28:02 -0700 (PDT), Julio Di Egidio
<ju...@diegidio.name> wrote:

>On Friday, May 12, 2017 at 11:46:50 PM UTC+2, Doc O'Leary wrote:
><snip>
<snip>

>to suggest OO patterns to architect JS code is simple
>an utter mistake, that you propose so or that some vendors now are
>proposing so...

Are you saying ECMAScript programs can't or shouldn't implement an OO
design? If so, how do you justify your belief?


>That said, please do get a real name and email address: it is basic Usenet
>etiquette, indeed since the old days.

Anyone who knows a little about the internet can demonstrate that his
e-mail address is real and then find his other name within seconds.


>*Plonk*

Why bother telling us?

John

Julio Di Egidio

unread,
May 13, 2017, 10:17:18 AM5/13/17
to
On Saturday, May 13, 2017 at 3:39:44 PM UTC+2, John G Harris wrote:
> On Fri, 12 May 2017 17:28:02 -0700 (PDT), Julio Di Egidio
> <ju...@diegidio.name> wrote:
>
> >to suggest OO patterns to architect JS code is simple
> >an utter mistake, that you propose so or that some vendors now are
> >proposing so...
>
> Are you saying ECMAScript programs can't or shouldn't implement an OO
> design? If so, how do you justify your belief?

You may have beliefs, I am a software professional and that (what I said,
not how you rephrase it) is just the A in the ABC of JavaScript. That said,
I might explain more (to those who want to learn), not only about JS vs OO,
but also about where all that utter and utterly destructive bullshit comes
from, but you'll have to learn to ask questions honestly and politely first,
and show some respect for the discipline and then the profession, as you
would with your doctor or even just with the plumber (unless you are a
plumber).

Julio

Ben Bacarisse

unread,
May 13, 2017, 11:26:01 AM5/13/17
to
Julio Di Egidio <ju...@diegidio.name> writes:

> On Saturday, May 13, 2017 at 3:39:44 PM UTC+2, John G Harris wrote:
>> On Fri, 12 May 2017 17:28:02 -0700 (PDT), Julio Di Egidio
>> <ju...@diegidio.name> wrote:
>>
>> >to suggest OO patterns to architect JS code is simple
>> >an utter mistake, that you propose so or that some vendors now are
>> >proposing so...
>>
>> Are you saying ECMAScript programs can't or shouldn't implement an OO
>> design? If so, how do you justify your belief?
>
> You may have beliefs, I am a software professional and that (what I said,
> not how you rephrase it) is just the A in the ABC of JavaScript.

ECMAScript uses prototype-based inheritance rather than class-based
inheritance but that does not mean it is not and object-oriented
language not that one should not write object-oriented programs using
it.

From page 1 of ECMA 262 (6th edition):

"ECMAScript is an object-oriented programming language..."

Obviously you can't use the same design techniques as you would do with
class-based object languages, but that's not quite the same point.

<snip>
--
Ben.

Julio Di Egidio

unread,
May 13, 2017, 11:30:58 AM5/13/17
to
On Saturday, May 13, 2017 at 5:26:01 PM UTC+2, Ben Bacarisse wrote:
> Julio Di Egidio <ju...@diegidio.name> writes:
>
> > On Saturday, May 13, 2017 at 3:39:44 PM UTC+2, John G Harris wrote:
> >> On Fri, 12 May 2017 17:28:02 -0700 (PDT), Julio Di Egidio
> >> <ju...@diegidio.name> wrote:
> >>
> >> >to suggest OO patterns to architect JS code is simple
> >> >an utter mistake, that you propose so or that some vendors now are
> >> >proposing so...
> >>
> >> Are you saying ECMAScript programs can't or shouldn't implement an OO
> >> design? If so, how do you justify your belief?
> >
> > You may have beliefs, I am a software professional and that (what I said,
> > not how you rephrase it) is just the A in the ABC of JavaScript.
>
> ECMAScript uses prototype-based inheritance rather than class-based
> inheritance but that does not mean it is not and object-oriented
> language not that one should not write object-oriented programs using
> it.

Yes it does: *at least* read Crockford before you open your mouth again,
you *apparently* don't know what you are talking about.

Julio

Cezary Tomczyk

unread,
May 13, 2017, 11:56:51 AM5/13/17
to
I think you are judging people too fast. I suggest to meet someone a bit
more closely before write such a statement.

--
Cezary Tomczyk
http://www.ctomczyk.pl/
https://www.aslint.org/ - Accessibility validation tool

Julio Di Egidio

unread,
May 13, 2017, 12:04:53 PM5/13/17
to
You may think or indeed suggest what you like, including that I am "judging
people", that is your problem and that of an entire in-civilisation, indeed.
I even remarked that to mean what I said, that it wasn't simply an educated
guess: apparent if he or indeed you actually knew the least bit about the
matter under discussion.

And this is the last reply I give to such kind of contributions. If anybody
has anything technical, I am at my desk working anyway, otherwise just have
a nice weekend.

Julio

Ben Bacarisse

unread,
May 13, 2017, 3:55:32 PM5/13/17
to
I doubt he means it. He often flies off the handle.

Anyway, I've read Crockford and he also describes JavaScript (that's the
term he uses) as an object oriented language.

There can be no reasonable doubt that ECMAScript is an object-oriented
language (the language definition says so, as does Crockford -- Julio Di
Egidio's preferred authority) but there *are* very significant
differences between class-based and prototype-based inheritance (which
Crockford discusses of course) which we could have a productive
discussion about.

If Julio Di Egidio would actually engage in the debate, we might find
out what he was getting at with the remark that started this subthread.

Oh well... that's Usenet.

--
Ben.

Ben Bacarisse

unread,
May 13, 2017, 7:00:13 PM5/13/17
to
r...@zedat.fu-berlin.de (Stefan Ram) writes:

> Ben Bacarisse <ben.u...@bsb.me.uk> writes:
>>There can be no reasonable doubt that ECMAScript is an object-oriented
>>language
>
> Alan Kay coined the term »object-oriented« in 1967,
> and he wrote to me in an e-mail in 2003:

Wikipedia says he coined it with others when at Xerox PARC, but he
joined PARC in 1970 (again, just Wikipedia as the source). I'm not
saying you're wrong, just curious about the various sources.

> »OOP to me means only messaging, local retention and protection and
> hiding of state-process, and extreme late-binding of all things.
> It can be done in Smalltalk and in LISP. There are possibly
> other systems in which this is possible, but I'm not aware of them.«
>
> JavaScript already existed in 2003, but Alan Kay did not
> mention it as a System in which OOP can be done.

But what does it mean in general nowadays?

Interestingly, whilst JavaScript objects do not naturally lend
themselves to what I'll call for the moment "Kay object-oriented"
programming, there is more of a connection than many people might think.
That's because in Smalltalk (the archetype for "Kay object-oriented"
programming) the classes (to which objects belong) are themselves are
objects, so you have direct access to what is essentially a prototype in
JS.

--
Ben.

Julio Di Egidio

unread,
May 13, 2017, 9:39:48 PM5/13/17
to
You incompetent moron and stupid cunt, you should simply shut the fuck up:
you not only don't know what you are talking about, you apparently are so
stupid and biased that you cannot even read, or appreciate that you won't
understand this stuff in few minutes, and I mean not even if you weren't
the retarded cunt that you in fact are.

> If Julio Di Egidio would actually engage in the debate, we might find
>
> Oh well... that's Usenet.

Julio Di Egidio won't teach you any more shit unless you change your
attitude, because that's lesson number 1 for *any* discipline, you sucker.
But I have known *you* for years, and you are simply too stupid and too
coward to change your attitude, so just fuck you and *Plonk*

Julio

Ben Bacarisse

unread,
May 13, 2017, 10:07:26 PM5/13/17
to
r...@zedat.fu-berlin.de (Stefan Ram) writes:

> Ben Bacarisse <ben.u...@bsb.me.uk> writes:
>>r...@zedat.fu-berlin.de (Stefan Ram) writes:
>>>Ben Bacarisse <ben.u...@bsb.me.uk> writes:
>>>>There can be no reasonable doubt that ECMAScript is an object-oriented
>>>>language
>>>Alan Kay coined the term »object-oriented« in 1967,
>>>and he wrote to me in an e-mail in 2003:
>>Wikipedia says he coined it with others when at Xerox PARC, but he
>>joined PARC in 1970 (again, just Wikipedia as the source). I'm not
>>saying you're wrong, just curious about the various sources.
>
> He also wrote to me:
>
> |>When and where was the term "object-oriented" used first?
> |
> |At Utah sometime after Nov 66 when, influenced by Sketchpad, Simula,
> |the design for the ARPAnet, the Burroughs B5000, and my background in
> |Biology and Mathematics, I thought of an architecture for
> |programming. It was probably in 1967 when someone asked me what I was
> |doing, and I said: "It's object-oriented programming".
>
> (see the web for the complete e-mail)
>
>>That's because in Smalltalk (the archetype for "Kay object-oriented"
>>programming) the classes (to which objects belong) are themselves are
>>objects, so you have direct access to what is essentially a prototype in
>>JS.
>
> JavaScript is also inspired by Scheme, which is a kind of LISP.

Ah but, critically, not the sort that Kay was talking about. Scheme
does not have the sort of late binding of earlier LISPs. Obviously,
some late-binding mechanism could be implemented if an object-oriented
structure were built on top of Scheme, but it's not there at the core.

But JavaScript does have late binding of the sort in question.

> Kay wrote »LISP«, which is the classical spelling. Today, »Lisp«
> is used to refer to modern Lisps with CLOS. I am not sure whether
> Kay was referring to classical LISPs without CLOS, which would not
> make a lot of sense to me.

He was writing in 2003 so he was very likely reffering to Common Lisp
(but not, I think, to the cleaner Lisps). Whether CLOS itself meets his
strict definition, I'm not sure. I think it does, but an object system
that did could surely be written in Common Lisp.

> In Smalltalk, blocks of codes are objects too. This was missing in
> Java, C++, and JavaScript in 2003. But today all of these languages
> have lambda-like expression which can be use somewhat like
> smalltalk code blocks.

Right, though I don't think they are central to anyone's view of
object-oriented programming.

> Stefan Matthias Aust wrote a post in 2004 where he explained
> something like this: In Smalltalk, we can create a class
> »Boolean« and two subclasses »True« and »False«. In »True«
> we can define:
>
> ifTrue: aBlock
> ^aBlock value
>
> and in »False«
>
> ifTrue: aBlock
> ^nil
>
> . Now we have implemented an if statement using polymorphism.
> It can be used as
>
> a > 0 ifTrue: [a := 0]
>
> . »a > 0« will create an object of class »True« or of class
> »False«. This object then will receive the message »ifTrue:
> [a := 0]«. If the object has the class »True«, the block
> will be evaluated, otherwise »nil« will be returned. (End of
> what Stefan Matthias Aust explained, here given in my wordings.
> All errors in the explanation are mine [Stefan Ram's], not
> Stefan Matthias Aust's.)
>
> So in Smalltalk, even the control structures conceptually
> are implemented in terms of messages and polymorphism
> (while an implementation might not actually implement them
> this way for reasons of efficiency).

Yes. Something similar can be done (without classes) in the lambda
calculus. In JavaScript you could do this with

True = x => y => x
False = x => y => y

and then, if Boolean constructs gave True or False rather than
JavaScript's true or false, a conditional would be written

test (()=> then-code) (()=> else-code) ()

JavaScript's blocks are rather ugly since they require and empty
parameter list. Now, Boolean expressions don't yield True or False so
we'd have to write the if function like this:

If = Cond => Then => Else => (Cond ? True : False)(Then)(Else)()

so, finally, we can write

If (a > 0) (()=> a = 0) (()=> null)

It's not the same, of course, but I thought it would be useful to show
how one really does not need control structures at all!

<snip>
--
Ben.

John G Harris

unread,
May 14, 2017, 6:23:19 AM5/14/17
to
On Sat, 13 May 2017 18:39:43 -0700 (PDT), Julio Di Egidio
<ju...@diegidio.name> wrote:
>On Saturday, May 13, 2017 at 9:55:32 PM UTC+2, Ben Bacarisse wrote:

<snip>
>You incompetent moron and stupid cunt, you should simply shut the fuck up:
>you not only don't know what you are talking about, you apparently are so
>stupid and biased that you cannot even read, or appreciate that you won't
>understand this stuff in few minutes, and I mean not even if you weren't
>the retarded cunt that you in fact are.
>
>> If Julio Di Egidio would actually engage in the debate, we might find
>>
>> Oh well... that's Usenet.
>
>Julio Di Egidio won't teach you any more shit unless you change your
>attitude, because that's lesson number 1 for *any* discipline, you sucker.
>But I have known *you* for years, and you are simply too stupid and too
>coward to change your attitude, so just fuck you and *Plonk*

What a wonderful contribution to this discussion!

Can someone tell me what Julio is saying. So far I've gathered he says
ECMAScript is something, and also isn't it. Or is he only criticising
the JavaScript implementation of the language?

John

John G Harris

unread,
May 14, 2017, 6:36:57 AM5/14/17
to
On 13 May 2017 20:51:57 GMT, r...@zedat.fu-berlin.de (Stefan Ram)
wrote:

>Ben Bacarisse <ben.u...@bsb.me.uk> writes:
>>There can be no reasonable doubt that ECMAScript is an object-oriented
>>language
>
> Alan Kay coined the term »object-oriented« in 1967,
> and he wrote to me in an e-mail in 2003:
>
>»OOP to me means only messaging, local retention and protection and
>hiding of state-process, and extreme late-binding of all things.
>It can be done in Smalltalk and in LISP. There are possibly
>other systems in which this is possible, but I'm not aware of them.«
>
> JavaScript already existed in 2003, but Alan Kay did not
> mention it as a System in which OOP can be done.

In a video by one of the Smalltalk inventors, probably Alan Kay, the
speaker ended by saying that a language is Object Oriented *ONLY* if
it has *ALL* the features of Smalltalk.

So
Strongly typed languages are not OO.
Languages where a garbage collector is optional are not OO.
Languages which don't call methods by sending messages are not OO.

I think he was biased in favour of Smalltalk, too biased.

John

Ben Bacarisse

unread,
May 14, 2017, 7:34:11 AM5/14/17
to
Sure, but I don't think it matters too much because he's explicit about
the definition he's using (or, indeed, invented) so the discussion can't
be derailed by misunderstanding. I like it when people are passionate
about technical matters, and I like having definitions that stake out
the boundaries of concepts like this, even if they are not universally
agreed upon.

And I feel inclined to nit-pick a bit: I think you mean "static typing"
rather than "strong typing". What he's ruling out is code where the
types can be resolved by static analysis of the program text. A
language with dynamic or run-time types may enforce strong type
checking.

In fact, I think he's going further than this. His remark about very
late binding would rule out languages (like C++) where the compiler can
plant virtual tables making it seem like there is some run-time typing
going on. The acid test would be whether you can "wrap" a method at
run-time. You can do this in SmallTalk and CLOS (the Common List Object
System) and in Python as well.

--
Ben.

Thomas 'PointedEars' Lahn

unread,
May 14, 2017, 8:12:00 AM5/14/17
to
Ben Bacarisse wrote:

> John G Harris <ni...@jghnorth.org.uk.invalid> writes:
>> In a video by one of the Smalltalk inventors, probably Alan Kay, the
>> speaker ended by saying that a language is Object Oriented *ONLY* if
>> it has *ALL* the features of Smalltalk.
>>
>> So
>> Strongly typed languages are not OO.
>> Languages where a garbage collector is optional are not OO.
>> Languages which don't call methods by sending messages are not OO.
>>
>> I think he was biased in favour of Smalltalk, too biased.
>
> Sure, but I don't think it matters too much because he's explicit about
> the definition he's using (or, indeed, invented) so the discussion can't
> be derailed by misunderstanding. I like it when people are passionate
> about technical matters, and I like having definitions that stake out
> the boundaries of concepts like this, even if they are not universally
> agreed upon.
>
> And I feel inclined to nit-pick a bit: I think you mean "static typing"
> rather than "strong typing".

I think you mean static _type-checking_.

If anyone doubts that e.g. Java (which does have static type-checking) is
an object-oriented programming language, they did way too much Small talk.

Thomas 'PointedEars' Lahn

unread,
May 14, 2017, 9:40:30 AM5/14/17
to
Stefan Ram wrote:

> let result = decoratum.call( this, ...arguments );

let result = decoratum.apply(this, arguments);

> [rest of atrocious code snipped]

Ben Bacarisse

unread,
May 14, 2017, 10:37:33 AM5/14/17
to
Thomas 'PointedEars' Lahn <Point...@web.de> writes:

> Ben Bacarisse wrote:
>
>> John G Harris <ni...@jghnorth.org.uk.invalid> writes:
>>> In a video by one of the Smalltalk inventors, probably Alan Kay, the
>>> speaker ended by saying that a language is Object Oriented *ONLY* if
>>> it has *ALL* the features of Smalltalk.
>>>
>>> So
>>> Strongly typed languages are not OO.
>>> Languages where a garbage collector is optional are not OO.
>>> Languages which don't call methods by sending messages are not OO.
>>>
>>> I think he was biased in favour of Smalltalk, too biased.
>>
>> Sure, but I don't think it matters too much because he's explicit about
>> the definition he's using (or, indeed, invented) so the discussion can't
>> be derailed by misunderstanding. I like it when people are passionate
>> about technical matters, and I like having definitions that stake out
>> the boundaries of concepts like this, even if they are not universally
>> agreed upon.
>>
>> And I feel inclined to nit-pick a bit: I think you mean "static typing"
>> rather than "strong typing".
>
> I think you mean static _type-checking_.

Not really, though it would certainly do. I wanted to focus on the
static analysis of types more generally including include languages
without type declaration where the types are inferred. Obviously some
of these might also check some types, but if the semantics are very
forgiving there may nothing to "check" (in the usual sense of giving a
type error). I don't imagine there are any such languages, but it was
not the checking that mattered to me at the time I wrote the above.

> If anyone doubts that e.g. Java (which does have static type-checking) is
> an object-oriented programming language, they did way too much Small talk.

Quite. These days the term obviously doesn't mean what Kay wants it to
mean, and I doubt it did even at the time. Simula is usually described
as object-oriented and dates form the mod 1960s. It may be that no one
put the words "object" and "oriented" together before Kay did, but if
he'd asked the Simula people, in 1967, if their programs were
"object-oriented" I think they would have said yes because of the
obvious meaning of the juxtaposition.

--
Ben.

Thomas 'PointedEars' Lahn

unread,
May 14, 2017, 11:16:46 AM5/14/17
to
Ben Bacarisse wrote:

> Thomas 'PointedEars' Lahn <Point...@web.de> writes:
>> Ben Bacarisse wrote:
>>> And I feel inclined to nit-pick a bit: I think you mean "static typing"
>>> rather than "strong typing".
>> I think you mean static _type-checking_.
>
> Not really, though it would certainly do. I wanted to focus on the
> static analysis of types more generally including include languages
> without type declaration where the types are inferred. Obviously some
> of these might also check some types, […]

<https://en.wikipedia.org/wiki/Type_system#Type_checking>

>> If anyone doubts that e.g. Java (which does have static type-checking) is
>> an object-oriented programming language, they did way too much Small
>> talk.
>
> Quite. […]

This was partly tongue-in-cheek :)
0 new messages