Skip to content

Instantly share code, notes, and snippets.

@dmail
Last active August 26, 2016 11:06
Show Gist options
  • Save dmail/6e639ac50cec8074a346c9e10e76fa65 to your computer and use it in GitHub Desktop.
Save dmail/6e639ac50cec8074a346c9e10e76fa65 to your computer and use it in GitHub Desktop.
JavaScript Function cloning implementation and documentation
function cloneFunction(fn, mode = 'primitive') {
var clonedFn;
if (mode === 'primitive') {
clonedFn = fn;
} else if (mode === 'construct') {
clonedFn = new Function('return ' + fn.toString())();
} else if (mode === 'wrap') {
let Constructor;
clonedFn = function() {
if (this instanceof clonedFn) {
// some native constructor must absolutely be called using new (Array for instance)
// se we can't use Object.create but we need to be able to pass arbitrary number of arguments
// http://stackoverflow.com/questions/1606797/use-of-apply-with-new-operator-is-this-possible
if (Constructor === undefined) {
Constructor = function(args) {
return fn.apply(this, args);
};
Constructor.prototype = fn.prototype;
}
return new Constructor(arguments);
}
return fn.apply(this, arguments);
};
} else if (mode === 'bind') {
clonedFn = fn.bind(arguments[2]);
}
return clonedFn;
}
@dmail
Copy link
Author

dmail commented Aug 26, 2016

Every mode has a subset of the following features :

  • own properties -> means clonedFn can have its own properties (not shared with fn).
  • scope access -> means clonedFn have access to fn scope.
  • single call -> means calling clonedFn involves only one function call.
  • works with native -> means cloning a native function (such as Array) will work as expected.
  • own this -> means clonedFn can have its own this
mode own properties scope access single call works with native own this
'primitive' no yes yes yes no
'construct' yes no yes no no
'wrap' yes yes no yes no
'bind' yes yes no yes yes

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