On Aug 17, 5:51 pm, Matt Kruse <[EMAIL PROTECTED]> wrote:
> Assigning event functions like click() require an anonymous function
> for what is often a very small snippet of code. These anonymous
> functions are confusing to inexperienced javascript coders and make
> the code less readable, IMO.

People who are unwilling to become comfortable with the language
they're working in (e.g., by using its available features, such as
anonymous functions) shouldn't be working in the language.

> I think it would be great to be able to pass a string to these
> functions and have it internally turned into a function.

i think this would be counter-intuitive. If i pass a string to a
function and know that it will be executed, i would expect the string
to be eval()'d, not run in a Function object, and those evaluate their
code with different scoping rules.

> This simple code change seems to do the job:
> // Handle event binding
> jQuery.fn[o] = function(f){
>         return f ? this.bind(o, ((typeof f=="string")?Function(f):f)) :
> this.trigger(o);
> };

Shouldn't that be (new Function(f)) instead of (Function(f))?

> Is it possible to change jQuery to accept a string in addition to a
> function reference where possible? Although you would lose the
> potential benefit of the closure by passing a string, you could always
> pass the function ref instead if you needed to.

IMO this change would be far more complex that it seems because, for
example, the following no longer applies:

if( typeof f == "function" ) { ... }

That would have to be changed to include checks for Function:

if( (typeof f == "function") || (f instanceof Function) )

and the call syntax for those are different. You can, as far as i
know, simply do f() when f is an instance of Function (i may be wrong
on that, but i don't believe JS offers any sort of operator
overloading for built-in types).

Not only would the core be affected, but plugin authors might also
have to go back and make similar changes, for consistency. The cascade
of side-effects is enormous.

i personally see no benefit, and several potential down-sides, to this
change.

Reply via email to