Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Mixin on objects #14

Open
xeoncross opened this issue May 26, 2012 · 1 comment
Open

Using Mixin on objects #14

xeoncross opened this issue May 26, 2012 · 1 comment

Comments

@xeoncross
Copy link

I'm doubt this is an issue, probably just a problem with what I'm doing but I can't get mixins to work using the default method.

MicroEvent.mixin    = function(destObject){
    var props   = ['bind', 'unbind', 'trigger'];
    for(var i = 0; i < props.length; i ++){
        destObject.prototype[props[i]]  = MicroEvent.prototype[props[i]];
    }
}

When trying with an object

var obj = {
    foo : 'bar',
     method : function() { }
};
    MicroEvent.mixin(obj);

I get can't convert undefined to object. If I change the method to this though it works:

MicroEvent.mixin2 = function(destObject)
{
    for (var k in MicroEvent.prototype)
    {
        if (MicroEvent.prototype.hasOwnProperty(k))
        {
            destObject[k] = MicroEvent.prototype[k];
        }
    }
}
@yckart
Copy link

yckart commented Mar 13, 2013

Yeah, problem is, that you cannot set the event as prototype on the destObject, so just test if it is a function or not and handle it:

destObject = typeof destObject === "function" ? destObject.prototype : destObject;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants