> I think that would be quite different from the way map currently
> works, which does not modify the array passed in but returns a new
> array whose values are the results of applying the callback function
> to each successive value of the original one.

Maybe jQuery could first clone the object:

var newObj = $.extend( {}, obj );
( It's just for the purpose of demonstration, it might have an issue
of efficiency )

> I'm not making much sense out of that.  Are you assuming that every
> property of the "obj" parameter is a function?  Can you give some
> context and a more real-world example?  This does not sound hard to
> write, but I'm not seeing the benefits.

One scenario is that it will be useful, if we want to modify options a
user passed in, which is normally an object and have callbacks in it.

> I don't know what scoping issue you're talking about, but I think that
> the only real way to enumerate the properties of an object involves
> for-in.

This is what I meant:

Let's say this is the options a user passed in:
var options = {
    key: 'value',
    callback1: function() {}
    callback2: function() {}
}

And we want to modify the callback:
( This snippet won't work )

for( key in options ) {
    var option = options[ key ]
    if( $.isFunction( option ) )
        options[ key ] = function() {
            // Call old callback
            option();

            // Do something else
        }
}

It won't work because the "option" here is not scoped, so all modified
callbacks will call whatever callback that is the last one being
iterated.

And it should be written like this:

for( key in options ) {
    // Scope them
    (function() {
        var option = options[ key ]
        if( $.isFunction( option ) )
            options[ key ] = function() {
                // Call old callback
                option();

                // Do something else
            }

    })();
}

If $.map() is able to iterate over objects, it won't have the issue of
scoping:

$.map( options, function( option ) {
    // No need to scope, because it already does
    if( $.isFunction( option ) )
        return function() {
            // Call old callback
            option();

            // Do something else
        }
})

I feel it's a lot neater.

--

You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to jquery-...@googlegroups.com.
To unsubscribe from this group, send email to 
jquery-dev+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en.


Reply via email to