PDA

View Full Version : [DEFER][2.x,3.x] createDelegate doesn't allow original scope



Condor
9 Dec 2009, 4:44 AM
Specifying 'null' as the scope for createDelegate will call the function with the scope set to 'window' instead of 'this' (see this post (http://www.extjs.com/forum/showthread.php?p=416979#post416979) for an example).
createCallback also uses 'window' instead of 'this' as the scope (I can't think of an example, but I recommend fixing it anyway).

It should be:

Ext.apply(Function.prototype, {
createDelegate : function(obj, args, appendArgs){
var method = this;
return function() {
var callArgs = args || arguments;
if (appendArgs === true){
callArgs = Array.prototype.slice.call(arguments, 0);
callArgs = callArgs.concat(args);
}else if (Ext.isNumber(appendArgs)){
callArgs = Array.prototype.slice.call(arguments, 0);
var applyArgs = [appendArgs, 0].concat(args);
Array.prototype.splice.apply(callArgs, applyArgs);
}
return method.apply(obj || this || window, callArgs);
};
},
createCallback : function(){
var args = arguments,
method = this;
return function() {
return method.apply(this || window, args);
};
}
});
(createInterceptor and createSequence already do this)

mystix
9 Dec 2009, 8:29 AM
can this ever be falsey?

i ask because if it's never so, then we could omit window as a fallthrough option.

Condor
10 Dec 2009, 7:36 AM
can this ever be falsey?

i ask because if it's never so, then we could omit window as a fallthrough option.

It could be falsey if explicitly call()ed or apply()ed with undefined/null/false.

I just copied this from createInterceptor and createSequence, so if we decide to drop the window scope fallthough then these methods should also be modified.