1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| Function.prototype.createDelegate(obj,args) {
var This = this; args=args||[];
return function() {
var newArgs = new Array();
for (var i=0; i<args.length; i++) { newArgs.push(args[i]); }
for (var i=0; i<arguments.length; i++) { newArgs.push(arguments[i]); }
return This.apply(obj, newArgs);
};
}
// Plus tard; Dans une fonction où this existe
setTimeout(this.maMethode.createDelegate(this, [true, 0, 'abc']), 1000)
// Ou alors
var f = this.maMethode.createDelegate(this)
setTimeout(function() { f(true, 0, 'abc'); }, 1000)
// Ou encore
var f = this.maMethode.createDelegate(this, [true, 0])
setTimeout(function() { f('abc'); }, 1000)
// Ceci marche aussi
document.body.onmousedown=this.maMethode.createDelegate(this); |
Partager