javascript multiple inheritance code review.

61 views
Skip to first unread message

Jaison Justus

unread,
Jun 28, 2012, 7:14:51 AM6/28/12
to
i had made a small code snippet for multiple inheritance in js. this code is working for me but actually i like to get the review, that its good and if any problem; provide the solution. etc. the code snippet is below.

/**
* WebbaseUtility provide the bundle of utilities used for support the webbase
* functionalities;
* @package : WebbaseUtility;
* @version : 1.0.0;
*/
var WebbaseUtility = {};

/**
* ObjectExt module extends the object properties and functionalities like inheritance etc;
* @module : ObjectExt;
* @package : Webbase;
* @version : 1.0.0;
* @return Object;
*/

var ObjectExt = WebbaseUtility.ObjectExt = (function() {

/**
* ObjectExt Constructor;
*/
var ObjectExt = function() {
};

/**
* This method provide inheritance to the object supplied; this method inherit the
* public methods from the Parent class to the Child class. this also provide
* multiple inheritance, in which the method ambiguity is solved by the overriding
* the last inherited class's method;
* @access public;
* @method inherit;
* @param Object Parent;
* @param Object Child;
* @return Object;
*/
ObjectExt.prototype.inherit = function(Parent, Child) {
var TempObj = function(){}, MultipleInheritanceTempObj = function(){};

MultipleInheritanceTempObj.prototype = Child.prototype;
TempObj.prototype = Parent.prototype;

for(var key in MultipleInheritanceTempObj.prototype) {
TempObj.prototype[key] = MultipleInheritanceTempObj.prototype[key];
}
MultipleInheritanceTempObj = null;
Child.prototype = new TempObj();

if(Child.uber === undefined) {
Child.uber = Parent.prototype;
}else {
for(var key in Parent.prototype) {
Child.uber[key] = Parent.prototype[key];
}
}
Child.prototype.constructor = Child;

return Child;
};

return new ObjectExt();

})();

Child = WebbaseUtility.ObjectExt.inherit(ParentA, Child);
Child = WebbaseUtility.ObjectExt.inherit(ParentB, Child);

Michael Haufe (TNO)

unread,
Jun 29, 2012, 8:04:49 PM6/29/12
to
These types of utility classes show up very often. I would be surprised if there is much energy in the group to review at Yet Another Mixin Library and repeat some old arguments.

More constructively, I would suggest looking at some of the related solutions/research of other JavaScript users such as:

<http://webreflection.blogspot.com/search?q=mixin>

and,

<https://groups.google.com/forum/?fromgroups#!searchin/comp.lang.javascript/mixin>

Message has been deleted

Scott Sauyet

unread,
Jul 5, 2012, 7:15:44 AM7/5/12
to
Jaison Justus wrote:
> i had made a small code snippet for multiple inheritance
> in js. this code is working for me but actually i like to
> get the review, that its good and if any problem; provide
> the solution. etc. the code snippet is below.

I apologize for this fairly empty response. I've tried twice recently
to post fairly detailed responses to this, and both got swallowed by
the netgods. I'm trying again through a different means. If this one
is successful, I will probably try one more time to reconstruct more
more detailed response, but I dont' want to go through that process if
some malicious force is simply keeping my voice out of this
thread! :-)

But so that there's actual content here, a basic objection to your
code is that you are updating the parent prototype object because this
line just sets a reference:

Child.uber = Parent.prototype;

and then this one updates the object that Child.uber refers to:

Child.uber[key] =
Parent.prototype[key];

So in trying to extend Parent, you're actually manipulating it.

-- Scott
Message has been deleted

Scott Sauyet

unread,
Jul 5, 2012, 9:24:55 AM7/5/12
to
Jaison Justus wrote:
> i had made a small code snippet for multiple inheritance
> in js. this code is working for me but actually i like to
> get the review, that its good and if any problem; provide
> the solution. etc. the code snippet is below.

I'm going to do this in pieces. I tried again with my longer response
and Google Groups once again decided to eat it. I really don't know
why.

My first thought is that Michael is right and you should look at some
of the available mixin libraries. There are many mixin techniques
available. And on top of that, there are many interesting approaches
layered on top of them. Peter Michaux published one recently [1] on
applying mixins to constructor functions. Angus Croll published one
on creating functions out of mixins [2], and together with Dan Webb he
also presented this technique [3] at the recent Fluent Conference. I
published a rough draft of an article on a still different approach to
mixins. [4]

Next up: structural critiques of the function.
-- Scott Sauyet

[1] http://peter.michaux.ca/articles/mixins-and-constructor-functions
[2] http://javascriptweblog.wordpress.com/2011/05/31/a-fresh-look-at-javascript-mixins/
[3] https://speakerdeck.com/u/anguscroll/p/how-we-learned-to-stop-worrying-and-love-javascript
[4] http://scott.sauyet.com/Javascript/Drafts/2012-05-04a/

Scott Sauyet

unread,
Jul 5, 2012, 11:21:06 AM7/5/12
to
Jaison Justus wrote:
> i had made a small code snippet for multiple inheritance
> in js. this code is working for me but actually i like to
> get the review, that its good and if any problem; provide
> the solution. etc. the code snippet is below.

Again, every time I try to enter my full critique it's disappeared, so
this is my second of three separate responses. The first suggested
that you look at other mixin implementations. This one looks at the
structure of your function. The final one looks at a fundamental
issue with the function itself.

> [whitespace and comments removed -- SDS]

> var WebbaseUtility = {};
> var ObjectExt = WebbaseUtility.ObjectExt = (function () {
> var ObjectExt = function () {
> };
> ObjectExt.prototype.inherit = function (Parent, Child) {
> var TempObj = function () {
> }, MultipleInheritanceTempObj = function () {
> };
> MultipleInheritanceTempObj.prototype = Child.prototype;
> TempObj.prototype = Parent.prototype;
> for (var key in MultipleInheritanceTempObj.prototype) {
> TempObj.prototype[key] = MultipleInheritanceTempObj.prototype[key];
> }
> MultipleInheritanceTempObj = null;
> Child.prototype = new TempObj();
> if (Child.uber === undefined) {
> Child.uber = Parent.prototype;
> } else {
> for (var key in Parent.prototype) {
> Child.uber[key] = Parent.prototype[key];
> }
> }
> Child.prototype.constructor = Child;
> return Child;
> };
> return new ObjectExt();
> })();

I don't understand the reason for your complex syntactic structure.
You assign your ObjectExt variable to the result of an immediately
invoked function expression (IIFE). That IIFE involves defining a
constructor function, attaching the `inherit` function to its
prototype`, and then constructing and returning an instance of that
constructor:

| var ObjectExt = (function () {
| var ObjectExt = function () { };
| ObjectExt.prototype.inherit = function (Parent, Child) {
| // content here
| };
| return new ObjectExt();
| })();

I don't see that any of that serves any purpose over the much simpler
version here:

| var ObjectExt = {inherit: function (Parent, Child) {
| // content here
| };

Can you give any reason for the existence of this constructor
function, the IIFE, the prototype, etc? It seems to be plain
overhead, and serves only to obfuscate your code.

One other minor point is that you probably should declare your `key`
variable once in the top of the `inherit` function.

Up next: a more fundamental critique of the function.

-- Scott

Scott Sauyet

unread,
Jul 5, 2012, 11:23:29 AM7/5/12
to
I wrote:
>|    var ObjectExt = {inherit: function (Parent, Child) {
>|        // content here
>|    };
^
Correction:

>| }};
^
Message has been deleted
Message has been deleted

Scott Sauyet

unread,
Jul 5, 2012, 4:35:23 PM7/5/12
to
Jaison Justus wrote:
> i had made a small code snippet for multiple inheritance
> in js. this code is working for me but actually i like to
> get the review, that its good and if any problem; provide
> the solution. etc. the code snippet is below.

I have tried over and over to post a response here on the fundamental
issues with this code and been frustrated at every turn. I have no
idea why. I noticed that this same question was posted on Stack
Exchanges CodeReview forum. If anyone is interested in my response to
the substantive problem with the code sample in question, it's
available at the URL below. I'm sorry not to repeat it here, but
whenever I do, the post never seems to show up.

<http://codereview.stackexchange.com/questions/13147/javascript-
multiple-inheritance-code-review/13375#13375>


-- Scott

Stefan Weiss

unread,
Jul 5, 2012, 6:10:17 PM7/5/12
to
On 2012-07-05 22:35, Scott Sauyet wrote:
> I have tried over and over to post a response here on the fundamental
> issues with this code and been frustrated at every turn. I have no
> idea why.

FWIW, my news provider seems to have received all of your posts without
trouble. I see 9 replies from you in this thread, including the first
one with the full code example. On Google Groups, only five replies show up.

I hardly ever open Google Groups anymore, because I get angry every time
I see the barrage of spam this group receives. I'm angry at Google,
because they are ones responsible for it. Almost every single spam
message was sent from a Google account. They're doing a perfectly fine
job of keeping spam from GMail, so why not use the same technology for
Groups? Not even searching works reliably - in a Google product! And why
conflate Usenet with their own groups?
I tried to give them the benefit of the doubt, applied Hanlon's Razor
and all that, but after all these years I'm pretty convinced they just
want to watch Usenet burn.

If you're interested in a good Usenet provider, I can warmly recommend
the one I'm using: http://albasani.net/ provides free (r/w) access to
text groups. It's run by two Usenet enthusiasts from Austria, and
probably the best thing I've ever used to access newsgroups (in 15 years
and counting). I had to scroll back a full month to find the most recent
spam message that got through their filter.

No web interface though, you'll need a proper client.

Many people can't use NNTP from work, so they're often stuck with Google
Groups. There are commercial Usenet providers who offer free or very
cheap (3-4$ a month) access to text groups, and they usually have web
interfaces, too. The last one I used (Giganews) didn't have very good
spam filters, but there are many others. Some of them also serve NNTP on
port 80, which may or may not get past the firewalls at work.

> <http://codereview.stackexchange.com/questions/13147/javascript-
> multiple-inheritance-code-review/13375#13375>

Oh, a new StackExchange site? Haven't seen this one before.
Not too much traffic yet, but it's still in beta.


- stefan

Scott Sauyet

unread,
Jul 6, 2012, 7:02:42 AM7/6/12
to
Stefan Weiss wrote:
> Scott Sauyet wrote:
>
>> I have tried over and over to post a response here on the fundamental
>> issues with this code and been frustrated at every turn.  I have no
>> idea why.
>
> FWIW, my news provider seems to have received all of your posts without
> trouble. I see 9 replies from you in this thread, including the first
> one with the full code example. On Google Groups, only five replies show up.

Thank you for the update. I will definitely try another web-based
USENET provider. I know I've been lazy about it. Lately I've been
looking at setting up my own mini-NNTP site for my own private use
talking to eternal-september or someone, and just running it over
HTTP, but everytime I got started it just didn't seem worth the
hassle. I post from such varied locations that I really need to be
able to do it via a web-browser, and that means that I can't do it
with a better client. Arggh!

You're right about Google Groups. They obviously have the means to
filter spam; why won't they apply it here?

Thanks again for the update. I'll stop spamming the group with the
same damn post. (I wonder in how many ways I managed to phrase the
same basic ideas?! :-) )

-- Scott

Scott Sauyet

unread,
Jul 6, 2012, 7:43:50 AM7/6/12
to
Scott Sauyet wrote:
> Stefan Weiss wrote:
>> Scott Sauyet wrote:
>>
>>> I have tried over and over to post a response here on the fundamental
>>> issues with this code and been frustrated at every turn.  I have no
>>> idea why.
>>
>> FWIW, my news provider seems to have received all of your posts without
>> trouble. I see 9 replies from you in this thread, including the first
>> one with the full code example. On Google Groups, only five replies show
up.
>
> Thank you for the update. I will definitely try another web-based
> USENET provider.

Hey, look, they are all here! I still have to decide on what provider to use,
but at least this one doesn't make all the old posts disappear!

I apologize for the repetition to this group.

-- Scott

Gene Wirchenko

unread,
Jul 6, 2012, 1:47:16 PM7/6/12
to
On Fri, 6 Jul 2012 04:02:42 -0700 (PDT), Scott Sauyet
<scott....@gmail.com> wrote:

[snip]

>Thank you for the update. I will definitely try another web-based
>USENET provider. I know I've been lazy about it. Lately I've been
>looking at setting up my own mini-NNTP site for my own private use
>talking to eternal-september or someone, and just running it over
>HTTP, but everytime I got started it just didn't seem worth the
>hassle. I post from such varied locations that I really need to be
>able to do it via a web-browser, and that means that I can't do it
>with a better client. Arggh!

I use eternal-september with Forte's Agent for a newsreader. This
works well for me. The price is nice (free), and Ray Banana who runs
it gives good, friendly service.

[snip]

Sincerely,

Gene Wirchenko

Jaison Justus

unread,
Jul 7, 2012, 7:25:18 AM7/7/12
to
Thanks Scott Sauyet. Thank you for your wonderful reply. this is actually what i like to know. being a js starter and lover, i like to learn more from the experts. i am developing a js library name Webbase.js, you can access it from github (https://github.com/jaisonjustus/Webbase.js). i planned to improve my js skill by doing a library. if you dont mind can you join the project for reviewing.

Dr J R Stockton

unread,
Jul 8, 2012, 2:22:45 PM7/8/12
to
In comp.lang.javascript message <bccaefa0-81ed-43de-ac4f-ce2e4200be8e@go
oglegroups.com>, Sat, 7 Jul 2012 04:25:18, Jaison Justus
<jaison.j...@gmail.com> posted:
If you are a js starter, it is several years too soon for it to be fair
to offer a library for others to use. That calls for mature experience,
not trial and error.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

Scott Sauyet

unread,
Jul 9, 2012, 11:26:50 AM7/9/12
to
Dr J R Stockton wrote:
> Jaison Justus posted:
>
>> [ ... ] being a js starter and lover, i like to
>> learn more from the experts. i am developing a js library name
>> Webbase.js, you can access it from github (https://github.com/jaisonjus
>> tus/Webbase.js). i planned to improve my js skill by doing a library.
>
> If you are a js starter, it is several years too soon for it to be fair
> to offer a library for others to use. That calls for mature experience,
> not trial and error.

I'm going to disagree here. Jaison shouldn't expect many people to use this
code unless it proves itself pretty quickly, but writing a reusable library
is a great way of thinking about how to write good code. Doing so in a
public forum is an especially bold way to work with a new technology. As
long as the author is willing to accept and learn from criticism, this might
work out well.

-- Scott

Dr J R Stockton

unread,
Jul 10, 2012, 4:20:52 PM7/10/12
to
In comp.lang.javascript message <201207091...@webnews.fastusenet.o
rg>, Mon, 9 Jul 2012 15:26:50, Scott Sauyet <3103i...@webnews.fastuse
net.org> posted:
That's OK and interesting for us; it is the people who hit upon it by a
Web search and naively believe it to be soundly designed that I am
thinking about [*]. Those who are practising should endeavour to do so
without collateral damage.

[*] <FAQENTRY>
Believe nothing that appears in this or any other newsgroup, unless
either you have reason to repose confidence in the author, or you have
at least waited a few days to see what follow-up articles may say.

Aside : does anyone here fancy reading some bits of Latin & French?

--
(c) John Stockton, nr London UK ?@merlyn.demon.co.uk IE8 FF8 Op11 Sf5 Cr15
news:comp.lang.javascript FAQ <http://www.jibbering.com/faq/index.html>.
<http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

Jaison Justus

unread,
Jul 19, 2012, 7:19:31 AM7/19/12
to
On Monday, 9 July 2012 18:26:50 UTC+3, Scott Sauyet wrote:
> Dr J R Stockton wrote:
> &gt; Jaison Justus posted:
> &gt;
> &gt;&gt; [ ... ] being a js starter and lover, i like to
> &gt;&gt; learn more from the experts. i am developing a js library name
> &gt;&gt; Webbase.js, you can access it from github (https://github.com/jaisonjus
> &gt;&gt; tus/Webbase.js). i planned to improve my js skill by doing a library.
> &gt;
> &gt; If you are a js starter, it is several years too soon for it to be fair
> &gt; to offer a library for others to use. That calls for mature experience,
> &gt; not trial and error.
>
> I&#39;m going to disagree here. Jaison shouldn&#39;t expect many people to use this
> code unless it proves itself pretty quickly, but writing a reusable library
> is a great way of thinking about how to write good code. Doing so in a
> public forum is an especially bold way to work with a new technology. As
> long as the author is willing to accept and learn from criticism, this might
> work out well.
>
> -- Scott

Your right Scott, but my ultimate aim is to deliver to the public but before that i need to learn by practice. new patterns, techniques, team collaborations. these things cannot be learn from reading blog and forums without practicing it. that's why i started a project and learn from it. so i can realize my pros and cons in coding and realize my own growth... thank you for your post..
Reply all
Reply to author
Forward
0 new messages