Javascript Multiple Inheritance Code snippet review?
-
i had made a small code snippet for multiple inheritance in js. this code is in working condition. but actually i like to get the review on that it don't have any performance issues or it the not the right way.. like that and if any problem; provide the solution. etc. the code snippet is given below. i tried in stackoverflow but no response. i think it may be due to my language. if any issue regardind my language please revert me back /** * 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);
-
Answer:
Given a cursory look, this is not actually in "working condition" as you've claimed. Running the code as is results in: ReferenceError: ParentA is not defined Next, there is a lot of line clutter and mixed whitespace that makes it nearly impossible to read. Take a look: Once the code is reformatted into something readable, it becomes clear that the order that the extended objects are handled is backwards. Commonly, the target is to the left of the super class, here the new class "B" will inherit from the existing class "A": class B extends A This is upheld in many common JavaScript libraries, "B" is the target object, "A" is the source: _.extend( B, A ); jQuery.extend( B, A, ... ); util.inherits( B, A ); dojo.declare( B, A, ... ); The ES6 class proposal is also in this form: class A { constructor() { this.prop = null; } set( val ) { this.prop = val; } get() { return this.prop; } } class B extends A {} let b = new B(); b.prop; // null b.set("hi!"); b.get(); // "hi!" The next most obvious thing is that your comments say that Parent and Child are objects when they should really be labelled "function object" or "constructor", but because you're example isn't actually functional, there is no way to tell what ParentA, ParentB or Child really is; function object, prototype object, ordinary object...? Beyond that, there are no tests and no specification of your goals, so it's hard to say whether it's right or wrong.
Rick Waldron at Quora Visit the source
Related Q & A:
- How to review a web application code?Best solution by Stack Overflow
- Why is Javascript called Javascript, if it has nothing to do with Java?Best solution by Stack Overflow
- How to encrypt JavaScript code?Best solution by Stack Overflow
- How do you set up a trust fund for an inheritance?Best solution by citywire.co.uk
- Does one pay taxes on an inheritance from overseas?Best solution by finweb.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.