Niccaman Posted July 18, 2011 Share Posted July 18, 2011 Lets say I have: function example(){ // code 1 } ...already declared in the document. Now i need to insert some new code as an extension of the 'example()' so that it will run example() and then my newer code. For example, I know this isnt possible, but I wanna do this: example.extend(function(){ // code 2 }); ... so that when I call 'example()' later, it will run both code 1 AND code 2. Quote Link to comment https://forums.phpfreaks.com/topic/242278-is-this-possible/ Share on other sites More sharing options...
.josh Posted July 18, 2011 Share Posted July 18, 2011 <script type='text/javascript'> // original function function someFunction () { document.write('original function<br/>'); } // copy to something else var original_someFunction = someFunction; // new function: make it like this... var someFunction = function() { // call original function original_someFunction(); document.write('new function<br/>'); }; // example... someFunction(); </script> Quote Link to comment https://forums.phpfreaks.com/topic/242278-is-this-possible/#findComment-1244350 Share on other sites More sharing options...
Niccaman Posted July 18, 2011 Author Share Posted July 18, 2011 haha ofcourse! wp mate. Quote Link to comment https://forums.phpfreaks.com/topic/242278-is-this-possible/#findComment-1244352 Share on other sites More sharing options...
goodacre.liam Posted July 20, 2011 Share Posted July 20, 2011 If you want to create new instances of functions which extend another, the following could help: //@Using Function prototype Function.prototype.extend = function (_callback) { var self = this; return function () { self(); _callback(); }; } //@Using Function - Preferred Method Function.extend = function (_base, _callback) { return function () { _base(); _callback(); }; } //@Example var func1, func2, func3; // Base function func1 = function () { console.log('FUNC1'); }; // Using prototype extend func2 = func1.extend(function () { console.log('FUNC2'); }); // Using Function extend - Preferred Method func3 = Function.extend(func2, function () { console.log('FUNC3'); }); I wouldn't use the Function.prototype method, as modifying core object prototypes is usually a bad idea. But extending Function is perfectly fine. In the examples above, func1 would log FUNC1, func2 would log FUNC1, FUNC2, and func3 would log FUNC1, FUNC2, and FUNC3. If you want to attach methods automatically, for example: func1.extend(function () { // extension code }) And you want func1 to be modified, you could write a new function builder, which stores a chain of methods. As such: //@Method builder var Method = function (_func) { var chain = [_func]; var core = function () { for (var index = 0, max = chain.length; index < max; ++index) { chain[index](); } }; core.extend = function (_func) { chain.push(_func); }; return core; }; //@Demo usage var func1; func1 = new Method(function () { console.log('Running func1'); }); func1.extend(function () { console.log('Extended'); }); func1.extend(function () { console.log('And Again!'); }); func1(); // logs 'Running func1', 'Extended', and 'And Again!' Hope this helps! Quote Link to comment https://forums.phpfreaks.com/topic/242278-is-this-possible/#findComment-1245282 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.