Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #14744 > unrolled thread
| Started by | Jaison Justus <jaison.justus.lp@gmail.com> |
|---|---|
| First post | 2012-06-28 04:14 -0700 |
| Last post | 2012-07-19 04:19 -0700 |
| Articles | 20 — 8 participants |
Back to article view | Back to comp.lang.javascript
javascript multiple inheritance code review. Jaison Justus <jaison.justus.lp@gmail.com> - 2012-06-28 04:14 -0700
Re: javascript multiple inheritance code review. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2012-06-29 17:04 -0700
Re: javascript multiple inheritance code review. Scott Sauyet <scott.sauyet@gmail.com> - 2012-06-29 21:59 -0700
Re: javascript multiple inheritance code review. Scott Sauyet <scott.sauyet@gmail.com> - 2012-07-05 04:15 -0700
Re: javascript multiple inheritance code review. Scott Sauyet <scott.sauyet@gmail.com> - 2012-07-05 05:06 -0700
Re: javascript multiple inheritance code review. Scott Sauyet <scott.sauyet@gmail.com> - 2012-07-05 06:24 -0700
Re: javascript multiple inheritance code review. Scott Sauyet <scott.sauyet@gmail.com> - 2012-07-05 08:21 -0700
Re: javascript multiple inheritance code review. Scott Sauyet <scott.sauyet@gmail.com> - 2012-07-05 08:23 -0700
Re: javascript multiple inheritance code review. Scott Sauyet <scott.sauyet@gmail.com> - 2012-07-05 08:32 -0700
Re: javascript multiple inheritance code review. Scott Sauyet <scott.sauyet@gmail.com> - 2012-07-05 13:21 -0700
Re: javascript multiple inheritance code review. Scott Sauyet <scott.sauyet@gmail.com> - 2012-07-05 13:35 -0700
Re: javascript multiple inheritance code review. Stefan Weiss <krewecherl@gmail.com> - 2012-07-06 00:10 +0200
Re: javascript multiple inheritance code review. Scott Sauyet <scott.sauyet@gmail.com> - 2012-07-06 04:02 -0700
Re: javascript multiple inheritance code review. "Scott Sauyet" <3103invalid@webnews.fastusenet.org> - 2012-07-06 11:43 +0000
Re: javascript multiple inheritance code review. Gene Wirchenko <genew@ocis.net> - 2012-07-06 10:47 -0700
Re: javascript multiple inheritance code review. Jaison Justus <jaison.justus.lp@gmail.com> - 2012-07-07 04:25 -0700
Re: javascript multiple inheritance code review. Dr J R Stockton <reply1227@merlyn.demon.co.uk.not.invalid> - 2012-07-08 19:22 +0100
Re: javascript multiple inheritance code review. "Scott Sauyet" <3103invalid@webnews.fastusenet.org> - 2012-07-09 15:26 +0000
Re: javascript multiple inheritance code review. Dr J R Stockton <reply1228@merlyn.demon.co.uk.not.invalid> - 2012-07-10 21:20 +0100
Re: javascript multiple inheritance code review. Jaison Justus <jaison.justus.lp@gmail.com> - 2012-07-19 04:19 -0700
| From | Jaison Justus <jaison.justus.lp@gmail.com> |
|---|---|
| Date | 2012-06-28 04:14 -0700 |
| Subject | javascript multiple inheritance code review. |
| Message-ID | <7533f63c-29ac-4765-a915-0511a7c7556b@googlegroups.com> |
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);
[toc] | [next] | [standalone]
| From | "Michael Haufe (TNO)" <tno@thenewobjective.com> |
|---|---|
| Date | 2012-06-29 17:04 -0700 |
| Message-ID | <5ecad12d-e276-41bb-9cb9-0979882839c4@googlegroups.com> |
| In reply to | #14744 |
On Thursday, June 28, 2012 6:14:51 AM UTC-5, 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.
>
> /**
> * 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);
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>
[toc] | [prev] | [next] | [standalone]
| From | Scott Sauyet <scott.sauyet@gmail.com> |
|---|---|
| Date | 2012-06-29 21:59 -0700 |
| Message-ID | <45022de2-3914-4dd5-b3dd-4934cb2d465b@d24g2000yqh.googlegroups.com> |
| In reply to | #14744 |
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.
[ code reformatted, and comments stripped -- Scott]
> 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();
> })();
>
> Child = WebbaseUtility.ObjectExt.inherit(ParentA, Child);
> Child = WebbaseUtility.ObjectExt.inherit(ParentB, Child);
Michael may be right; people here may be tired of these. Please do
look at the various mixin solutions that are available. There are
many solutions available. Peter Michaux wrote a very interesting
article [1] on related techniques. Angus Croll has an article on
using mixins as functions [2] and a presentation (with Dan Webb) from
Fluent [3] on a similar topic. I have the first draft of an article
[4] that I've never gotten around to completing on a still different
approach.
But on to the code itself.
First of all, is there any good reason to expose your single function
as a prototype method on the single object created from an unexposed
constructor function? It seems a lot of overkill. So instead of
this:
| var WebbaseUtility = {};
| var ObjectExt = WebbaseUtility.ObjectExt = (function() {
| var ObjectExt = function() {};
| ObjectExt.prototype.inherit = function(Parent, Child) {
| // ...
| };
| return new ObjectExt();
| })();
Couldn't you just do this?:
| var WebbaseUtility = {ObjectExt: {inherit : function(Parent,
Child) {
| // ...
| }}};
But my biggest issue is with these lines:
| if (Child.uber === undefined) {
| Child.uber = Parent.prototype;
| } else {
| for (var key in Parent.prototype) {
| Child.uber[key] = Parent.prototype[key];
| }
| }
When you come back through the else block, you will be assigning
properties to Child.uber, which is just a reference to
Parent.prototype. So this code will actually change the
Parent.prototype! That is utterly, horribly wrong.
Here's an example:
var Pet = function() {};
var WalkingAnimal = function() {};
WalkingAnimal.prototype.walk = function() {
console.log(this.name + " is walking")
};
var SwimmingAnimal = function() {};
SwimmingAnimal.prototype.swim = function() {
console.log(this.name + " is swimming")
};
var Dog = function(name) {this.name = name;}
Dog = WebbaseUtility.ObjectExt.inherit(Pet, Dog);
Dog = WebbaseUtility.ObjectExt.inherit(WalkingAnimal, Dog);
var ClownFish = function(name) {this.name = name;}
ClownFish = WebbaseUtility.ObjectExt.inherit(
Pet, ClownFish);
ClownFish = WebbaseUtility.ObjectExt.inherit(
SwimmingAnimal, ClownFish);
var dog = new Dog("Rover");
var fish = new ClownFish("Nemo");
dog.walk() // "Rover is walking
fish.swim() // "Nemo is swimming
fish.walk() // "Nemo is walking // WTF???
Pet.prototype was updated with WalkingAnimal's `walk` method. You
definitely need to rethink this a bit.
Good luck,
-- Scott
[1] http://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/
[toc] | [prev] | [next] | [standalone]
| From | Scott Sauyet <scott.sauyet@gmail.com> |
|---|---|
| Date | 2012-07-05 04:15 -0700 |
| Message-ID | <99c6ff25-4419-4889-98d6-64681521a6c1@o4g2000yqk.googlegroups.com> |
| In reply to | #14744 |
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
[toc] | [prev] | [next] | [standalone]
| From | Scott Sauyet <scott.sauyet@gmail.com> |
|---|---|
| Date | 2012-07-05 05:06 -0700 |
| Message-ID | <9a0fd69f-9eb5-484c-b0ed-bc62685520e4@37g2000yqu.googlegroups.com> |
| In reply to | #14744 |
(A quick note: The earlier post worked. I guess the netgods are no
longer fighting me. If this is a duplicate [triplicate?], please
forgive me. Neither of my earlier attempts seem to have come through
here.)
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.
[comments and whitespace 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();
> })();
First of all, I think Michael's advice is good. There are many good
mixin implementations out there. Why not examine some of those to see
if they will work for you? Recently Peter Michaux has published [1]
an interesting article on how to use mixins on constructor objects.
Angus Croll has one on a functional mixin pattern [2], which he also
presented along with Dan Webb [3] at the recent Fluent Conference.
And I have a first draft of a somewhat different take on mixins [4]
that I hope to someday finish.
But on to your code.
My first question is about overall structure. There is a lot of
overhead for creating a simple object with a single function on it.
And it seems quite unnecessary. Why all this boilerplate?:
> var WebbaseUtility = {};
> var ObjectExt = WebbaseUtility.ObjectExt = (function () {
> var ObjectExt = function () {};
> ObjectExt.prototype.inherit = function (Parent, Child) {
> // real content here
> };
> return new ObjectExt();
> })();
when the code really boils down to:
> var WebbaseUtility = {ObjectExt: {inherit: function (Parent, Child) {
> // real content here
> }};
plus a one-liner if you really do want to expose ObjectExt globally as
well? What is the point of creating an anonymous constructor
function, adding a function to its prototype, creating an instance
from that constructor, and then returning that instance from an
immediately-invoked function expression? I see no gain, just lots of
syntactic noise. But maybe this has been factored out of a larger
system where this made more sense?
Another minor point is that you probably should declare `key` once at
the top of the `inherit` function, just as good practice.
The major problem, though is much deeper, and strikes at the heart of
your core function:
| if (Child.uber === undefined) {
| Child.uber = Parent.prototype; // (1)
| } else {
| for (var key in Parent.prototype) {
| Child.uber[key] = Parent.prototype[key]; // (2)
| }
| }
In the line I've marked (1), you are assigning Child.uber *by
reference* to Parent.prototype. Then later, in line (2) you update
Child.uber. That means you're updating Parent.prototype. This is a
fatal flaw in your function. A child inheriting from a second parent
will add its second parents properties onto its first parent's
prototype.
Here is one case to demonstrate the issue:
var Pet = function(name) {this.name = name;}
Pet.prototype.speak = function(){
console.log(this.name + " speaks.");
};
var WalkingAnimal = function() {};
WalkingAnimal.prototype.walk = function() {
console.log(this.name + " is walking.");
};
var SwimmingAnimal = function() {};
SwimmingAnimal.prototype.swim = function() {
console.log(this.name + " is swimming.");
};
var Dog = function(name) {this.name = name;};
WebbaseUtility.ObjectExt.inherit(Pet, Dog);
WebbaseUtility.ObjectExt.inherit(WalkingAnimal, Dog);
var ClownFish = function(name) {this.name = name;};
WebbaseUtility.ObjectExt.inherit(Pet, ClownFish);
WebbaseUtility.ObjectExt.inherit(SwimmingAnimal, ClownFish);
var rover = new Dog("Rover");
var nemo = new ClownFish("Nemo");
rover.walk(); // "Rover is walking";
nemo.swim(); // "Nemo is swimming";
nemo.walk(); // "Nemo is walking"; // WTF?
So do look at other mixin's to see if they might already have the
functionality you want.
Good luck,
-- Scott
[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/
[toc] | [prev] | [next] | [standalone]
| From | Scott Sauyet <scott.sauyet@gmail.com> |
|---|---|
| Date | 2012-07-05 06:24 -0700 |
| Message-ID | <289755d0-93b0-4100-9644-d0ca9b51ad20@t20g2000yqn.googlegroups.com> |
| In reply to | #14744 |
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/
[toc] | [prev] | [next] | [standalone]
| From | Scott Sauyet <scott.sauyet@gmail.com> |
|---|---|
| Date | 2012-07-05 08:21 -0700 |
| Message-ID | <01577719-aed6-45fe-8962-f03519f4e7bc@j25g2000yqn.googlegroups.com> |
| In reply to | #14744 |
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
[toc] | [prev] | [next] | [standalone]
| From | Scott Sauyet <scott.sauyet@gmail.com> |
|---|---|
| Date | 2012-07-05 08:23 -0700 |
| Message-ID | <477773dd-d701-4d7e-9255-e7666a62f989@a34g2000yqi.googlegroups.com> |
| In reply to | #14873 |
I wrote:
>| var ObjectExt = {inherit: function (Parent, Child) {
>| // content here
>| };
^
Correction:
>| }};
^
[toc] | [prev] | [next] | [standalone]
| From | Scott Sauyet <scott.sauyet@gmail.com> |
|---|---|
| Date | 2012-07-05 08:32 -0700 |
| Message-ID | <af62a09d-ba9b-4a09-91ab-e091e95a7ef2@h9g2000yqi.googlegroups.com> |
| In reply to | #14744 |
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.
> [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();
> })();
Again, every time I try to enter my full critique it's disappeared, so
this is my third of three separate responses. The first suggested
that you look at other mixin implementations. The second one looked
at the structure of your function. This one looks at a fundamental
issue with the function itself.
The biggest problem is that you add properties to the Parent.prototype
object:
| if (Child.uber === undefined) {
| Child.uber = Parent.prototype; // (1)
| } else {
| for (var key in Parent.prototype) {
| Child.uber[key] = Parent.prototype[key]; // (2)
| }
| }
In the line I've marked (1), you assign Child.uber as a *reference* to
Parent.prototype, and then when you come back through the if block in
the line marked (2), you add properties to it. You have corrupted the
parent prototype object in this process. This is a fundamental flaw.
You will need to rethink this approach.
Here is an example to demonstrate the problem:
var Pet = function(name) {this.name = name;}
Pet.prototype.speak = function(){
console.log(this.name + " speaks.");
};
var WalkingAnimal = function() {};
WalkingAnimal.prototype.walk = function() {
console.log(this.name + " is walking.");
};
var SwimmingAnimal = function() {};
SwimmingAnimal.prototype.swim = function() {
console.log(this.name + " is swimming.");
};
var Dog = function(name) {this.name = name;};
WebbaseUtility.ObjectExt.inherit(Pet, Dog);
WebbaseUtility.ObjectExt.inherit(WalkingAnimal, Dog);
var ClownFish = function(name) {this.name = name;};
WebbaseUtility.ObjectExt.inherit(Pet, ClownFish);
WebbaseUtility.ObjectExt.inherit(SwimmingAnimal, ClownFish);
var rover = new Dog("Rover");
var nemo = new ClownFish("Nemo");
rover.walk(); // "Rover is walking";
nemo.swim(); // "Nemo is swimming";
nemo.walk(); // "Nemo is walking"; // WTF?
Please do look at some of the existing mixin implementations.
Good luck,
-- Scott
[toc] | [prev] | [next] | [standalone]
| From | Scott Sauyet <scott.sauyet@gmail.com> |
|---|---|
| Date | 2012-07-05 13:21 -0700 |
| Message-ID | <3f7a71fd-684a-476b-9578-61183cfa2dca@n32g2000yqd.googlegroups.com> |
| In reply to | #14744 |
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 third of three separate responses. The first suggested
that you look at other mixin implementations. The second one looked
at the structure of your function. This one looks at a fundamental
issue with the function itself.
And it disappeared on me again when I first tried to post it. I'm
going to post only my example that shows the flaw without any of my
commentary. I'm sorry about all these posts. I've been using Google
Groups for years with almost no issues (except that I think I've been
killfiled by a few regulars here.) This might make me finally try to
find an alternative that will work in the various locked-down
corporate environments I often end up working in. It's been very
frustrating. In any case, here's the example, based upon the code
that's been posted earlier, and which I've quoted earlier in this
thread:
var Pet = function(name) {this.name = name;}
Pet.prototype.speak = function(){
console.log(this.name + " speaks.");
};
var WalkingAnimal = function() {};
WalkingAnimal.prototype.walk = function() {
console.log(this.name + " is walking.");
};
var SwimmingAnimal = function() {};
SwimmingAnimal.prototype.swim = function() {
console.log(this.name + " is swimming.");
};
var Dog = function(name) {this.name = name;};
WebbaseUtility.ObjectExt.inherit(Pet, Dog);
WebbaseUtility.ObjectExt.inherit(WalkingAnimal, Dog);
var ClownFish = function(name) {this.name = name;};
WebbaseUtility.ObjectExt.inherit(Pet, ClownFish);
WebbaseUtility.ObjectExt.inherit(SwimmingAnimal, ClownFish);
var rover = new Dog("Rover");
var nemo = new ClownFish("Nemo");
rover.walk(); // "Rover is walking";
nemo.swim(); // "Nemo is swimming";
nemo.walk(); // "Nemo is walking"; // WTF?
This should demonstrate a real problem with the handling of
Parent.prototype in the original code. I apologize for not quoting it
again.
-- Scott
[toc] | [prev] | [next] | [standalone]
| From | Scott Sauyet <scott.sauyet@gmail.com> |
|---|---|
| Date | 2012-07-05 13:35 -0700 |
| Message-ID | <6a20b632-f998-49da-bdb4-826a01c5e49e@w6g2000yqg.googlegroups.com> |
| In reply to | #14744 |
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
[toc] | [prev] | [next] | [standalone]
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Date | 2012-07-06 00:10 +0200 |
| Message-ID | <jt53c9$kgg$1@news.albasani.net> |
| In reply to | #14885 |
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
[toc] | [prev] | [next] | [standalone]
| From | Scott Sauyet <scott.sauyet@gmail.com> |
|---|---|
| Date | 2012-07-06 04:02 -0700 |
| Message-ID | <4219d5dd-1cba-4f11-9832-12d197d1a232@37g2000yqu.googlegroups.com> |
| In reply to | #14886 |
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
[toc] | [prev] | [next] | [standalone]
| From | "Scott Sauyet" <3103invalid@webnews.fastusenet.org> |
|---|---|
| Date | 2012-07-06 11:43 +0000 |
| Message-ID | <201207061143493103@webnews.fastusenet.org> |
| In reply to | #14901 |
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
[toc] | [prev] | [next] | [standalone]
| From | Gene Wirchenko <genew@ocis.net> |
|---|---|
| Date | 2012-07-06 10:47 -0700 |
| Message-ID | <n19ev7l7rlg9hksl4ers26d9np8k50i7ad@4ax.com> |
| In reply to | #14901 |
On Fri, 6 Jul 2012 04:02:42 -0700 (PDT), Scott Sauyet
<scott.sauyet@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
[toc] | [prev] | [next] | [standalone]
| From | Jaison Justus <jaison.justus.lp@gmail.com> |
|---|---|
| Date | 2012-07-07 04:25 -0700 |
| Message-ID | <bccaefa0-81ed-43de-ac4f-ce2e4200be8e@googlegroups.com> |
| In reply to | #14744 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <reply1227@merlyn.demon.co.uk.not.invalid> |
|---|---|
| Date | 2012-07-08 19:22 +0100 |
| Message-ID | <iPdXqZH1$c+PFwSA@invalid.uk.co.demon.merlyn.invalid> |
| In reply to | #14920 |
In comp.lang.javascript message <bccaefa0-81ed-43de-ac4f-ce2e4200be8e@go oglegroups.com>, Sat, 7 Jul 2012 04:25:18, Jaison Justus <jaison.justus.lp@gmail.com> posted: >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/jaisonjus >tus/Webbase.js). i planned to improve my js skill by doing a library. >if you dont mind can you join the project for reviewing. 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)
[toc] | [prev] | [next] | [standalone]
| From | "Scott Sauyet" <3103invalid@webnews.fastusenet.org> |
|---|---|
| Date | 2012-07-09 15:26 +0000 |
| Message-ID | <201207091526503103@webnews.fastusenet.org> |
| In reply to | #14960 |
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
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <reply1228@merlyn.demon.co.uk.not.invalid> |
|---|---|
| Date | 2012-07-10 21:20 +0100 |
| Message-ID | <dIoo7wIk6I$PFw80@invalid.uk.co.demon.merlyn.invalid> |
| In reply to | #14969 |
In comp.lang.javascript message <201207091526503103@webnews.fastusenet.o rg>, Mon, 9 Jul 2012 15:26:50, Scott Sauyet <3103invalid@webnews.fastuse net.org> posted: >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. 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.
[toc] | [prev] | [next] | [standalone]
| From | Jaison Justus <jaison.justus.lp@gmail.com> |
|---|---|
| Date | 2012-07-19 04:19 -0700 |
| Message-ID | <2f598c61-ab9f-44ee-87bf-5f497b695713@googlegroups.com> |
| In reply to | #14969 |
On Monday, 9 July 2012 18:26:50 UTC+3, Scott Sauyet wrote: > 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 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..
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web