Martin,

I recently wrote a module object (class) that I use with prototypal 
inheritance. I chose to use Object.create (and define it if not available); but 
also, chose to create an extend method to add an object literal to the newly 
created object which inherits the object I am extending.

Code : 
        https://gist.github.com/1292234

/**
 * @method to create submodules / subclasses
 * @param o {Object} literal object with properties/methods as subclass
 */
PXHLR.Module.prototype.extend = function (subclass) {
        var mod = Object.create(this);
        mod = $.extend(mod, subclass);
        return mod;
};

Unit test  with console logging using #debug in the URL with open console in 
your browser:
        http://skript.co/test/gist-1292234/#debug

The unit tests in the test.js file show how I'm using this module object e.g. 
...

PXHLR.modules = new PXHLR.Module();
PXHLR.modules.init();
PXHLR.modules.defaults = { context : "#qunit-fixture" };
// extend module object
PXHLR.modules.subclass = PXHLR.modules.extend({
        beforeBinding : function (subclassObj) {
                this.debug("subclass beforeBinding");
                this._parent.beforeBinding(subclassObj);
        }
});
PXHLR.modules.subclass.init();

I did not want to use new (constructor) for each instance and reassign the 
constructor.prototype each time. I also wanted to have nested objects 
(subclasses) that can use the methods of the parent objects via lookup in the 
objects' prototype chain.

Perhaps the code and unit test examples will be useful in working out the 
inheritance structure you are working on.

Best regards,

Bill Heaton


On Oct 22, 2011, at 4:12 AM, Martin wrote:

> The following snippet shows how I've created my inheritance structure.
> I got inspired by this article.
> 
> http://www.kevlindev.com/tutorials/javascript/inheritance/inheritance10.htm
> 
> Code snippet: http://jsfiddle.net/v5x6w/1/
> 
> When this js file is loaded by the browser all the contructors will be
> called. As you can see this leads to some unexpected behaviour.
> 
> The base contructor is inadvertently called twice.
> 
> Is there any way to stop this behaviour ?
> 
> -- 
> To view archived discussions from the original JSMentors Mailman list: 
> http://www.mail-archive.com/[email protected]/
> 
> To search via a non-Google archive, visit here: 
> http://www.mail-archive.com/[email protected]/
> 
> To unsubscribe from this group, send email to
> [email protected]

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to