I'm not sure what "illegal Javascript syntax" is.  I just tested
Jörn's code and it runs without errors, and lets you do this:

$(thing).click(myFunc.bind(scope));

Instead of "illegal", can we just call it very very risky to extend
the prototypes of native JS objects?  :)  Altering the behavior of
native prototypes can lead to new and exciting bugs in other
libraries.  These are nigh impossible to track down, since they are
caused by completely unrelated code.  This sorrow is sometimes called
"action at a distance".

If you want to extend the prototype of Function, I won't tell on
you.  :)  Here is a perfectly safe alternative though:

function bind(fn, scope) {
return function() { fn.apply(scope, arguments) };
}

$(thing).click(bind(myFunc, scope));

David


On Feb 21, 1:27 am, RobG <[EMAIL PROTECTED]> wrote:
> On Feb 20, 9:17 pm, darki777 <[EMAIL PROTECTED]> wrote:
>
> > Hi,
> > is there any method to bind and create closures with "this" altered in
> > jQuery (with chain possibility)?? Need it on various functions. I do
> > not mean Events.
>
> > Anythink like this:
> > function myFunc()
> > {
>
> > }.bind(anything);
>
> That is illegal javascript syntax, the Function call or apply methods
> are provided for this:
>
>   function foo(){}
>   foo.call(anything);
>
> or perhaps:
>
>   var boundFoo = function(){
>     foo.apply(anything, arguments);
>   }
>
> --
> Rob

Reply via email to