It's very detailed, thank you for the demos! But I don't quite
understand the purpose of them. If you are saying $.map() shouldn't
work on objects because wrapping functions is not a good practice,
then $.map() should also not work on arrays. Here is why:

// options now is a sequence
var options = [ function() { this[1]() }}, function() { alert
('Oops') } ]

$.map() works on this "object", and it also causes the "problem" you
have illustrated. Actually, I don't perceive it as a "problem". What
you are doing is counting how many times the callback functions have
been called. Since some callback functions will call another callback
function, when three of them get called, it makes sense these callback
functions have been called six times in total.

Maybe I'm too dumb to get the essence of your demos, could you explain
more to me? Thanks.

On Oct 30, 12:55 pm, Scott Sauyet <scott.sau...@gmail.com> wrote:
> On Oct 29, 11:19 pm, gMinuses <gminu...@gmail.com> wrote:
>
> > But making map() functions work on hashes makes sense, so why rule
> > them out?
>
> I'm not convinced.  Is this the sort of usage, you are considering?:
>
>     var newMap = function(obj, fn) {
>         var options = $.extend( {}, obj);
>         for (key in options) {
>             options[key] = fn(options[key]);
>         }
>         return options;
>     };
>
>     $(document).ready(function() {
>         var obj = {
>             url: "http://jquery.com/";,
>             success: function() {alert("Hurrah!");},
>             failure: function() {alert("Boohoo!");}
>         };
>
>         var myInstrumentation = function(option) {
>             if ($.isFunction(option)) {
>                 return function() {
>                     if (!this.count) this.count = 0;
>                     this.count++;
>                     option.call(this);
>                 }
>             } else {
>                 return option;
>             }
>         };
>
>         var newObj = newMap(obj, myInstrumentation);
>
>         newObj.success.call(newObj);
>         newObj.failure.call(newObj);
>         newObj.success.call(newObj);
>
>         alert("Total calls: " + newObj.count);
>
> If so, then only a slight modification of the input object could cause
> real problems, e.g.
>
>         var obj = {
>             url: "http://jquery.com/";,
>             success: function() {
>                 this.helper();
>                 alert("Hurrah!");
>             },
>             failure: function() {
>                 this.helper();
>                 alert("Boohoo!");
>             },
>             helper: function() {
>                 // do something here
>             }
>         };
>
> Note that with this new function, count is also incremented in the
> nested calls to the helper function.
>
> Any time an object maintains its own state, or your mapping function
> maintains its own, your code is at the mercy of the implementation of
> the object supplied.  If there are only simple callback functions that
> don't call other functions in the object, you might not have problems,
> but in other cases, there could be problems.
>
> You can see these in action at
>
>    http://scott.sauyet.com/Javascript/Demo/2009-10-29a/1/ and
>    http://scott.sauyet.com/Javascript/Demo/2009-10-29a/2/
>
> As I said, I'm not convinced.  I think if you have a need for it, you
> can write your own extension quite easily, but I can't quite see
> something like this in the jQuery core.
>
> Cheers,
>
>   -- Scott

--

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