Yes i would recommend "apply" or closures - i'm not sure what about performance.. i barely remember there was a reason why closuers work better than binding "this", but i don't believe there is a real big deal which one you want. And closures just doesn't always makes sense because it's pretty tight bound to the architecture/design of your app.
Well first of all - i would never pass "this" just as a parameter of your function, javascript knows much better ways to achieve this:
function somePluginAfterSelectFeatureOne(param ){
// do stuff
}
function somePluginAfterSelectFeatureTwo(param){
// do stuff
}
$('.some-element').somePlugin({
afterSelect: function(param){
somePluginAfterSelectFeatureOne(param);
}
});
First, remove "this" as param from your function params, it's just wrong ;)
And now the call "somePluginAfterSelectFeatureOne(param)" will fail because, what would be this? Well "this" will lead to the scope which was valid when you defined your function (hard to explain, try to debug that down ;))
Anyways - to control the "this" of your function, you simply rewrite the call:
somePluginAfterSelectFeatureOne.apply(this, [param]);
Now, i guess the "this" which is already available inside of your "afterSelect: function(param){ }" call would be the correct one, right? So, by using the native function "apply", you're able to pass a new "this" to the called function and, still you're able to pass all normal params -> this is just the second param of "apply". But be aware, when using apply -> all params must be passed as an Array (since the internal "arguments" param which is always available on every function call, is in fact an array internally)
Now, this is still the solution with direct bind's - closures are still a different game:
$('.some-element').somePlugin({
afterSelect: function(param){
var _self = this,
myFunction = function(param) {
_self.anotherFn(param)
};
myFunction(param);
}
});
As you can see - we simply store "this" as a local variable called "_self".. Now, if the function definition is in the same scope like above -> "_self" will be available on it, even without passing it (that's what we call a "closure" ;)).
But as you can also see - it doesn't always make sense... in your example, you would be forced to move the function definitions inside of your "afterSelect" function, which is totally not the goal here.
So, i would go with an "apply" pattern and passing the "this" nicely - kind of a "delegate" ;)
Oh and to make a complete roundup - it's also possible to override "this" directly on function definition level. Use "bind" for this:
function myFunction(param) {
this.secondFunction(param)
}.bind(somethingDifferentWhichWillBeUsedAsThisInside)
I got also some all-time favorited threads regarding these two topics here: