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