Naming: createFunctionDelegate() vs createDelegateFunction()?
It appears that I am not able to choose between two names for a function:
createFunctionDelegate()
and createDelegateFunction()
.
If it matters, the purpose of the function is that it creates a new function that calls the supplied callback function in the context of the second argument. For example:
var foo = {
init: function() {
setTimeout(App.createFunctionDelegate(this.method, this));
},
method: function() {}
}
When foo.init()
is run, it sets a timeout that calls a function which delegates the execution to another function (this.method
) called in the context of this
(foo).
Anyway, I am not sure whic开发者_JAVA百科h way I should name this function. This is important to me, because I am going to use it in hundreds of places and sometimes I type the one and occasionally the other one. This has to change, I have to choose.
I would use neither of these. What you want to do will be offered by bind()
in ES5. I would define Function.prototype.bind
if it does not exist already, as described here (but read the description and the possible drawbacks carfully).
This way you make sure you use native functionality if it is supported.
What about just createDelegate(this.method, this)?
As a side note, other places where I've seen this kind of method (and written them), the param ordering is context, function, so createDelegate(this, this.method)
精彩评论