Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/242278-is-this-possible/
Share on other sites

<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>

 

 

Link to comment
https://forums.phpfreaks.com/topic/242278-is-this-possible/#findComment-1244350
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/242278-is-this-possible/#findComment-1245282
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.