Skip to content

Instantly share code, notes, and snippets.

@rhyolight
Created March 25, 2010 14:58
Show Gist options
  • Save rhyolight/343636 to your computer and use it in GitHub Desktop.
Save rhyolight/343636 to your computer and use it in GitHub Desktop.
function mixin(from, to) {
var property;
for (property in from) {
if (!to.hasOwnProperty(property)) {
to[property] = from[property];
}
}
return to;
}
@dshaw
Copy link

dshaw commented Apr 8, 2010

I figured that was the case.

@dshaw
Copy link

dshaw commented Apr 8, 2010

The apply function might be a better option altogether: https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Function/Apply

@rhyolight
Copy link
Author

I don't understand how this helps the mixin? Are you talking about calling the mixin method via apply like this:

function mixin(from) {
    var property;
    for (property in from) {
        if (!this.hasOwnProperty(property)) {
            this[property] = from[property];            
        }
    }
}

mixin.call(myInstance, superInstance);

(using https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Function/Call instead of apply so I don't have to wrap arg in [])

@rhyolight
Copy link
Author

Wow, so I tried the idea out:

http://s.dangertree.net/p/constr_perf5.png

@rhyolight
Copy link
Author

This is all relating back to http://rhyolight.posterous.com/javascript-constructor-performance (in case anyone reading was wondering)

@dshaw
Copy link

dshaw commented Apr 8, 2010

Re: apply

You're right, it's more appropriate for the classic prototypal inhertance.

@dshaw
Copy link

dshaw commented Apr 8, 2010

Re: performance. Woohoo, good deal! (Though I'm glad you simplified things a bit for the blog follow-up. I started getting lost in all those permutations.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment