vvo / chainit

Turn an asynchronous JavaScript api into an asynchronous chainable JavaScript api
Other
12 stars 4 forks source link

chain a subclass #11

Closed simoami closed 10 years ago

simoami commented 10 years ago

Consider the following inheritance patterns in Javascript:

The classic way:

var Base = function(config) { this.config = config };
Base.prototype.method1 = function(cb) { cb(); };
Base.prototype.method2 = function(cb) { cb(); };

var ChildClass = function(config) { this.config = config; this.prop = true; };
ChildClass.prototype = new Base();
ChildClass.prototype.constructor = ChildClass;

ChildClass.prototype.method3 = function(cb) { cb(); };
ChildClass.prototype.method4 = function(cb) { cb(); };

The constructor-less inheritance using ES5 Object.create

var Base = {
    create: function create() {
       var instance = Object.create(this);
       instance._construct.apply(instance, arguments);
       return instance;
    },
    extend: function extend(properties, propertyDescriptors) {
        propertyDescriptors = propertyDescriptors || {};
         if(properties){
            var simpleProperties = Object.getOwnPropertyNames(properties);
            for (var i = 0, len = simpleProperties.length; i < len; i += 1) {
                var propertyName = simpleProperties[i];
                if(propertyDescriptors.hasOwnProperty(propertyName)) {
                    continue;
                }
                propertyDescriptors[propertyName] =
                    Object.getOwnPropertyDescriptor(properties, propertyName);
            }
        }

        return Object.create(this, propertyDescriptors);
    },
     _construct: function _construct() {},

    method1: function method1(cb) { cb(); },
    method2: function method2(cb) { cb(); }
};

var ChildClass = Base.extend({
    method3: function(cb) { cb(); },
    method4: function(cb) { cb(); }
});

Can you recommend a good way to plug async chaining for ChildClass in both use-cases?

Thanks in advance!

vvo commented 10 years ago

@simoami I do not have an easy answer for you. If somethind is wrong with the lib, open another issue.

simoami commented 10 years ago

@vvo Thanks for replying. No worries. I've built this functionality here.