Sample code:
// some library defines a function:
var func1 = function() {
console.log('i am func1');
}
func1.magicProperty = 'bacon'; // with some properties
// i want to wrap the function to add some extra functionality:
(function() {
var original = func1;
func1 = function() {
var result = original.apply(this,arguments);
console.log('after func1');
return result;
};
})();
func1(); // yay it works!
console.log(func1.magicProperty); // but the properties are missing!!
How can I extend/wrap func1
but keep all its properties? Do I have to copy them all over to my new definition, or is there a better/faster way?
This is not quite the same as extending a [class] object because there's no usage of new
here.