Method overloading emulation.

A

Asen Bozhilov

Today i see couple of slides produced by John Resig. One of the
interesting code is addMethod to implement method overloading.
<URL: http://ejohn.org/apps/learn/#89 >

function addMethod(object, name, fn){
// Save a reference to the old method
var old = object[ name ];

// Overwrite the method with our new one
object[ name ] = function(){
// Check the number of incoming arguments,
// compared to our overloaded function
if ( fn.length == arguments.length )
// If there was a match, run the function
return fn.apply( this, arguments );

// Otherwise, fallback to the old method
else if ( typeof old === "function" )
return old.apply( this, arguments );
};
}

That method for my is excellent example for how *closures* can be
harmful. Assigned function to property have [[scope]] property which
refer Activation/Variable of current execution context. `old' property
of Activation/Variable contains previous value of that property. So
that produce long chain of Activation/Variable objects which isn't
marked for garbage collection on end of execution context.

e.g.
var foo = {};
addMethod(foo, 'bar', function(a){});
addMethod(foo, 'bar', function(a, b){});
addMethod(foo, 'bar', function(){});

1. inner function refer Activation/Variable from [[scope]] property
2. `old' refer function from step 1. inner function[[scope]] refer
ActivationVariable
3. `old' refer function from step 2. Activation/Variable which been
referred from inner function created in that execution context.
4. `bar' property of `object' who referred from `foo' will be refer
inner function created in step 3.

Who want design like this? If i have trusted to John Resig, maybe i
use that code, but for my this is junk. I don't like at all that idea
to emulate something which isn't supported from language native, but
someday if i want method overloading definitely i will be use
something like this one:

function Foo()
{
this._overloaded = {};
};

Foo.prototype = {
addMethod : function(name, fn)
{
if (!this._overloaded[name])
{
this._overloaded[name] = [];
this[name] = function()
{
var arg_len = arguments.length,
fn = this._overloaded[name][arg_len];
if (fn) return fn.apply(this, arguments);
}
}
if (fn instanceof Function)
{
this._overloaded[name][fn.length] = fn;
}
}
};

var foo = new Foo();
foo.addMethod('bar', function(){return 0;});
foo.addMethod('bar', function(a){return 1});
foo.addMethod('bar', function(a, b){return 2;});
window.alert(foo.bar()); //0
window.alert(foo.bar(10)); //1
window.alert(foo.bar(10, 20)); //2

Regards.
 
D

Dmitry A. Soshnikov

[...]
So that produce long chain of Activation/Variable objects which isn't
marked for garbage collection on end of  execution context.
[...]

Yep, the depth of the scope chain (on each step) will be directly
proportional to quantity of the added methods. Moreover, this version
uses linear search in added methods (in difference from yours version
with object and determination key as a value of the [length]
property).

So, if this book is yet draft, write to J.Resig and suggest it to him,
try to explain. But you know (saying by the secret ;)) I recently
added a comment to his post <URL: http://ejohn.org/blog/50-off-secrets-of-the-javascript-ninja/
about the book and my comment still is not published. J.Resig has
premoderation of the comments and seems he didn't like my comment in
which I say about misunderstanding of function [this-value] and how
(and when) it determinate. Just some days ago was talking about it
here: <URL:
http://groups.google.ru/group/comp....ff4e4/4fe99f1620c6d981?hl=en#4fe99f1620c6d981
. I wrote after some days a mail to Resig asking, why didn't he
published my comment, there's no answer yet. Maybe he treats wrong my
comment? But I just wanted to help to write it more accurate and
correctly if, repeat, it's still and yet a draft. Or maybe Resig likes
just good comments sort of: "what a wonderful book!", "waiting for
it!" - don't know. Just for info ;)

[...]
this._overloaded[name] = [];
[...]

Array can be sparse in here, better to use object.
 
A

Asen Bozhilov

Dmitry said:
So, if this book is yet draft, write to J.Resig and suggest it to him,
try to explain. But you know (saying by the secret ;)) I recently
added a comment to his post <URL:http://ejohn.org/blog/50-off-secrets-of-the-javascript-ninja/> about the book and my comment still is not published. J.Resig has

My english at the moment is not very well. I make dummy mistake, but
every Saturday i have english course. I hope soon my english to be a
better and members of c.l.js easy read my post. Whatever, if i write
to J.Resig i m not sure if i use terms from ECMA documentation he
understand me. That is joke and i hope is joke ; )
premoderation of the comments and seems he didn't like my comment in
which I say about misunderstanding of function [this-value] and how
(and when) it determinate. Just some days ago was talking about it
here: <URL:http://groups.google.ru/group/comp.lang.javascript/browse_thread/thre...>. I wrote after some days a mail to Resig asking, why didn't he
published my comment, there's no answer yet. Maybe he treats wrong my
comment? But I just wanted to help to write it more accurate and
correctly if, repeat, it's still and yet a draft. Or maybe Resig likes
just good comments sort of: "what a wonderful book!", "waiting for
it!" - don't know. Just for info ;)

That is discrimination. If you don't like criticism, that means you
don't want progress and at all that will be stop your progression. If
doesn't matter of professional area.
Unfortunately, that book will be translate to Bulgarian, because that
book is writing from author of JQuery. OMFG. Just like you say in
previous thread, that is Marketing and PR. And results from that is
one vicious circle. Bad books => bad explanations => bad examples =>
bad programmer => bad authors of book. It's a pity all of that,
because language is so so interesting.

[...]
this._overloaded[name] = [];
[...]
Array can be sparse in here, better to use object.

Yes, sometime that will be tread for benefit, but here i don't see
with can help me array instance. Only if implementation optimize speed
to access decimal properties of array objects?
I modified on my local machine and now i use object literal.
 
D

David Mark

[...]
So that produce long chain of Activation/Variable objects which isn't
marked for garbage collection on end of  execution context.
[...]

Yep, the depth of the scope chain (on each step) will be directly
proportional to quantity of the added methods. Moreover, this version
uses linear search in added methods (in difference from yours version
with object and determination key as a value of the [length]
property).

So, if this book is yet draft, write to J.Resig and suggest it to him,
try to explain. But you know (saying by the secret ;)) I recently
added a comment to his post <URL:http://ejohn.org/blog/50-off-secrets-of-the-javascript-ninja/> about the book and my comment still is not published.. J.Resig has

premoderation of the comments and seems he didn't like my comment in
which I say about misunderstanding of function [this-value] and how
(and when) it determinate.

Moderation is a great way to filter understanding while appearing to
know everything. It's typically done to prevent embarrassment of the
authors at the great expense of the community and users.
Just some days ago was talking about it
here: <URL:http://groups.google.ru/group/comp.lang.javascript/browse_thread/thre...>. I wrote after some days a mail to Resig asking, why didn't he

published my comment, there's no answer yet. Maybe he treats wrong my
comment? But I just wanted to help to write it more accurate and
correctly if, repeat, it's still and yet a draft. Or maybe Resig likes
just good comments sort of: "what a wonderful book!", "waiting for
it!" - don't know. Just for info ;)

He likely didn't understand you at all (and not because of your
English).
 
R

RobG

Today i see couple of slides produced by John Resig. One of the
interesting code is addMethod to implement method overloading.
<URL:http://ejohn.org/apps/learn/#89>

I would have expected that a presentation on overloading would:

1. Describe what overloading is
2. Present cases where it is useful
3. Present the negative side
4. Present alternatives
5. Described why a function to implement overloading is useful

I don't see any of that at the page linked, it seems more just a
collection of stuff. The example use of overloading is a poor
implementation of inheritance. The methods added using addMethods can
be written in fewer lines of clearer code if written using either
switch or if..else logic based on the provided arguments.

I might be nit-picking, but if someone claims to be a javascript
professional, and to have progressed to the ranks of "Ninja", then
their code should be exemplary.
 
D

David Mark

I would have expected that a presentation on overloading would:

1. Describe what overloading is
2. Present cases where it is useful
3. Present the negative side
4. Present alternatives
5. Described why a function to implement overloading is useful

I don't see any of that at the page linked, it seems more just a
collection of stuff. The example use of overloading is a poor
implementation of inheritance. The methods added using addMethods can
be written in fewer lines of clearer code if written using either
switch or if..else logic based on the provided arguments.

Yes, it is a perfectly useless example, especially for an audience
that is largely trying to come to grips with the language.
I might be nit-picking, but if someone claims to be a javascript
professional, and to have progressed to the ranks of "Ninja", then
their code should be exemplary.

Apologists may blame the marketing department responsible for that ill-
fated moniker, but his code and design are clearly awful and the
explanations are often fantasy. By any name, in any language, that's
a problem.
 
D

Dmitry A. Soshnikov

My english at the moment is not very well. I make dummy mistake, but
every Saturday i have english course. I hope soon my english to be a
better and members of c.l.js easy read my post. Whatever, if i write
to J.Resig i m not sure if i use terms from ECMA documentation he
understand me. That is joke and i hope is joke ; )

Your English is good enough to understand each other, sure I also make
wording mistakes and typos sometimes but most of people can understand
me, and I can understand you - that's the main goal ;) But if to talk
about some article (especially academical) then sure such typos and
mistakes are not acceptable.
premoderation of the comments and seems he didn't like my comment in
which I say about misunderstanding of function [this-value] and how
(and when) it determinate. Just some days ago was talking about it
here: <URL:http://groups.google.ru/group/comp.lang.javascript/browse_thread/thre...>. I wrote after some days a mail to Resig asking, why didn't he
published my comment, there's no answer yet. Maybe he treats wrong my
comment? But I just wanted to help to write it more accurate and
correctly if, repeat, it's still and yet a draft. Or maybe Resig likes
just good comments sort of: "what a wonderful book!", "waiting for
it!" - don't know. Just for info ;)

That is discrimination. If you don't like criticism, that means you
don't want progress and at all that will be stop your progression. If
doesn't matter of professional area.
Unfortunately, that book will be translate to Bulgarian, because that
book is writing from author of JQuery. OMFG. Just like you say in
previous thread, that is Marketing and PR. And results from that is
one vicious circle. Bad books => bad explanations => bad examples =>
bad programmer => bad authors of book. It's a pity all of that,
because language is so so interesting.

Funny, but I still want to believe that Resig has some other reason:
maybe he's on some business trip or sort of and has no time to analyze
corrections I wrote. I can even forgive if he's asking now somebody to
explain my corrections (if to suggest that he doesn't understand
that). But if he ignored it just because of afraid that everybody will
see that "Ninjas stuff" (in current draft) is not so "Ninjas" - that's
really bad and non-professional.

My purpose is always educational, I just suggest.

/ds
 
D

Dmitry A. Soshnikov

[...]
Moderation is a great way to filter understanding while appearing to
know everything.  It's typically done to prevent embarrassment of the
authors at the great expense of the community and users.

ha, that for cowards, right? :D kidding ;) I mean do not show the
*correct* but which you don't like comment - that's sort of weakness.
In my blog I do not remove any comment, even that say: "your articles
are too complex to understand, it's just crap!", 'cause it's also
meaning and that meaning shows that articles are good and just reader
is not ready yet for them. I have no premoderation also. That's all
regardless spam messages sure which are deletes, but in my blog
there's no spam at all.
He likely didn't understand you at all (and not because of your
English).

Do you think so? Maybe. But I think J.Resig knows some parts of
ECMA-262 (maybe not very deep, but - some of them). I've already
suggested, if Resig knows ES, then he writes in such simplified manner
(and with self-named terminology which not related to ECMA-262)
because wanna get more people involved and interested in this.
Moreover, the book is not about exactly deep ECMA-262 knowledges, but
about JS programming in a whole and from this point of view Resig
cannot be blamed, though the title of the book a bit pathos then ;)
 
D

Dmitry A. Soshnikov

[...]
1. Describe what overloading is

The stuff suggested by Resig is related to languages with strong type
system (as usually) - here overloading they need to describe several
methods but with different set of parameters, e.g. types of quantity
of parameters.
[...]
can be written in fewer lines of clearer code if written using either
switch or if..else logic based on the provided arguments.

Yes, exactly, overloading by parameters in ES could be achieved by
analyze `arguments.length` and `<functionObject>.length`. The only
"win" from having several functions for that - is that code will be
ran directly, without checking each time `if-else` or `switch-case`.
From the other hand, having several function objects - that more
expensive by the memory usage and sure variant with checking
`arguments.length` and so on is better.
I might be nit-picking, but if someone claims to be a javascript
professional, and to have progressed to the ranks of "Ninja", then
their code should be exemplary.

I repeat, seem the book is just has pathos title, but content is very
casual (though I didn't read the book a whole), but, we can't blame
Resig for that ;) And only if he will say that this is professional
book and "Ninja" on the title means "this book contains professional
info" - then will be another talk.
 
A

Asen Bozhilov

Dmitry said:
Funny, but I still want to believe that Resig has some other reason:
maybe he's on some business trip or sort of and has no time to analyze
corrections I wrote. I can even forgive if he's asking now somebody to
explain my corrections (if to suggest that he doesn't understand
that).

I hope so, he give answer on your mail or public your comment in his
blog. For me is interesting and i'll be checked J.Resig blog for him
answer. If you get response via e-mail, please public the
correspondence or forward me one copy. Thanks.
But if he ignored it just because of afraid that everybody will
see that "Ninjas stuff" (in current draft) is not so "Ninjas" - that's
really bad and non-professional.

If he is afraid for why will be publish that book? If he write books
for ES only for money, that is very bad for readers, because author
don't give a damn for own readers and what readers will be knowing
after they read *Secrets*. For ES i want book which based on ECMA 262
documentations. I want author give relevant explanations with words
and examples. I want author to say, what is the best practice from his
opinion and what is bad practices and why these practices is Best/Bad.
I want he give me things for i thinking about. And again if i see book
with title "professional", "secret", etc i'll be expected that book is
truly good stuff.
My purpose is always educational, I just suggest.
That is very good. I have same reason, i don't like the hate and
blame. In that thread <URL:
http://groups.google.bg/group/comp.lang.javascript/browse_thread/thread/26a9efc584bff4e4#>
i don't want to blame Andrea Giamarchi. Sometime i post junk comment
in his blog, because he is very very arrogant person and he don't like
critics. Just like you i want clear explanations.

Regards.
 
L

Lasse Reichstein Nielsen

Asen Bozhilov said:
And again if i see book
with title "professional", "secret", etc i'll be expected that book is
truly good stuff.

That's why marketing people puts it there :)

It's like country names. The more "people's" and "democratic" they
have in their names, the more likely they are to be totalitarian
dictatorships.

/L
 
D

Dmitry A. Soshnikov

I hope so, he give answer on your mail or public your comment in his
blog. For me is interesting and i'll be checked J.Resig blog for him
answer. If you get response via e-mail, please public the
correspondence or forward me one copy. Thanks.


If he is afraid for why will be publish that book? If he write books
for ES only for money, that is very bad for readers, because author
don't give a damn for own readers and what readers will be knowing
after they read *Secrets*. For ES i want book which based on ECMA 262
documentations. I want author give relevant explanations with words
and examples. I want author to say, what is the best practice from his
opinion and what is bad practices and why these practices is Best/Bad.
I want he give me things for i thinking about. And again if i see book
with title "professional", "secret", etc i'll be expected that book is
truly good stuff.

Here everything's not so easy, and from the other hand - is very easy
to understand why it's so. The main goal is in *type* of literature.
If to divide all the technical literature by and large, there're two
main parts - practical literature and theoretical literature.

Both of this parts can be *professional*. Moreover, for *-massive-
auditory (readers)* there's no book which is written only with one of
that parts. If it will be only practical book - that will be just
collection of "copy-paste" stuff (maybe junk-stuff). If it will be
only theoretical book - it will be too "dry" - and it's already not a
book, it's already e.g. technical standard with just algorithm
descriptions.

Sure, can be only theoretical book (exactly book, not like standard) -
but it's already irrelevant with "normal everyday life" (i mean
programming), it's already academical book and not is not intended to
*massive auditory*.

So every author can choose, in which style to write his book or
article (no matter).

If the reason to write it correctly and accurate (where the main
criterion is an *accuracy*), he will use terminology and descriptions
related to technology he describes. The price for that (always) - not
so big auditory (not so many listeners, readers). The reason is simple
- not everybody's interested in that deep theoretical stuff.

If the reason to make it more practical and relevant with normal
everyday programming, than author *should* explain as easy, as
possible. Which *means* (always), again for *massive auditory*, to use
simplified terminology and use 40% of pictures (usually funny) of all
book. And it really helps. The main criterion here is an *simplicity
of descriptions*. Here author himself can know all this stuff deeply,
but should explain in simplified manner. The price for that - is no
accurate terminology, but many people involved which then
(themselves), if will have interest, will learn it deeper. This stuff
is like learn-books for students. Because not all of them want to
learn this science deeply on theoretical level. And this practical
level can be also professional, though, people won't know what is
"Activation object" means - but they even don't wanna, and they -
don't need it.

So Resig's book is about second style. And from this point of view
(learning students for normal everyday practical programming) this
book can be called professional and we can't judge from the point of
first (theoretical) style, 'cause author has chosen the second style.

The thing is also related to *abstraction levels*. If some folk knows
ES deeply, he'll see the errors in some framework, saying - that's
wrong, that's not by spec, that's not optimal. But, some framework (or
some new language) - is just new layer of abstraction, which suggests
to write on that *new* level of abstraction without thinking how has
it been written inside. And from this point of view we can't judge
anybody who suggest such stuff, else the people from lower abstraction
level can say - you just look how bad is ES written itself inside,
what crazy and stupid constructions are used. On what we can say -
that's not interesting how concrete implementation is written, and the
same can say users of ExtJS (which suggests higher UI abstractions
level), jQuery, Prototype.js and so on - that's not interesting for us
how it was written, we operate on another abstraction level, saying
"Window", "Panel" and so on - just like it objects of the new
language. Everybody can choose himself - use it or not and by what
reason.

But sure, to mention mistakes and error in some art-stuff: books,
articles, frameworks, languages and so on, and to correct them (if you
are author) listening to good advises - that's *the main goal* of all
that stuff. That's the progress. Somebody does something, somebody
corrects, suggests better, first listen to him, make better, then the
third makes batter than both firsts and so on so on - that's life-
cycle of the progress.

So, backing to Resigs book, I understand the reasons why his book
contains that incorrect terms and meanings - he just want to involve
more people (and uses simplified terminology though maybe he knows how
it's indeed) or he really doesn't know all that and know it not-deep.
If the first reason, then my corrections with how it should be from
the point of ECMA-262-3 is not necessary, if the second one - that's
still bad that the comment isn't published.

But I suggest to stop discussion of the Resig as we're making
advertising and PR for him and in particular for that book ;) But, ok,
I'll send you a copy via mail if get the answer.
That is very good. I have same reason, i don't like the hate and
blame. In that thread <URL:http://groups.google.bg/group/comp.lang.javascript/browse_thread/thre...>
i don't want to blame Andrea Giamarchi. Sometime i post junk comment
in his blog, because he is very very arrogant person and he don't like
critics. Just like you i want clear explanations.

Ah, don't be worry about that, you both: you and Andrea know what you
where talking about and know ES well, so it's just a little
misunderstanding each other, no more, no less ;) So, nevermind (as
Andrea should also do so).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top