jashkenas / underscore

JavaScript's utility _ belt
https://underscorejs.org
MIT License
27.34k stars 5.53k forks source link

map is sometimes destructive #229

Closed janlimpens closed 13 years ago

janlimpens commented 13 years ago

var philosophers = ['bergson', 'platon', 'hume', 'marx', 'hegel', 'rousseau', 'aristoteles', 'russell', 'kant', 'nietzsche', 'freud', 'leibniz', 'wittgenstein', 'aquino']; function makeUrl(item){ return '++resource++ufscar.dfmc.plonetheme.images/headers/' + item + '.gif'; } var urls = _.map(philosophers, makeUrl);

leaves urls as undefined and when I check philosophers, it has been modified (as one would imagine url should look like). Is there some known issue that switches map into destructive mode? How could I check, if some other lib (this is in a plone application that calls dozens of other libs I have no knowledge of) somehow tampered with or .map? $.map gives me the expected result, so I use that inbetween, but would prefer to have this fixed....

_.map reads... function (a,c,b){var e=[];if(a==null)return e;if(t&&a.map===t)return a.map(c,b);h(a,function(a,g,G){e[e.length]=c.call(b,a,g,G)});return e}

see my console output below

var philosophers = ['bergson', 'platon', 'hume', 'marx', 'hegel', 'rousseau', 'aristoteles', 'russell', 'kant', 'nietzsche', 'freud', 'leibniz', 'wittgenstein', 'aquino']; function makeUrl(item){ return '++resource++ufscar.dfmc.plonetheme.images/headers/' + item + '.gif'; } var urls = $.map(philosophers, makeUrl); undefined urls ["++resource++ufscar.dfmc.plonetheme.images/headers/bergson.gif", "++resource++ufscar.dfmc.plonetheme.images/headers/platon.gif", "++resource++ufscar.dfmc.plonetheme.images/headers/hume.gif", "++resource++ufscar.dfmc.plonetheme.images/headers/marx.gif", "++resource++ufscar.dfmc.plonetheme.images/headers/hegel.gif", "++resource++ufscar.dfmc.plonetheme.images/headers/rousseau.gif", "++resource++ufscar.dfmc.plonetheme.images/headers/aristoteles.gif", "++resource++ufscar.dfmc.plonetheme.images/headers/russell.gif", "++resource++ufscar.dfmc.plonetheme.images/headers/kant.gif", "++resource++ufscar.dfmc.plonetheme.images/headers/nietzsche.gif", "++resource++ufscar.dfmc.plonetheme.images/headers/freud.gif", "++resource++ufscar.dfmc.plonetheme.images/headers/leibniz.gif", "++resource++ufscar.dfmc.plonetheme.images/headers/wittgenstein.gif", "++resource++ufscar.dfmc.plonetheme.images/headers/aquino.gif"] var philosophers = ['bergson', 'platon', 'hume', 'marx', 'hegel', 'rousseau', 'aristoteles', 'russell', 'kant', 'nietzsche', 'freud', 'leibniz', 'wittgenstein', 'aquino']; function makeUrl(item){ return '++resource++ufscar.dfmc.plonetheme.images/headers/' + item + '.gif'; } var urls = _.map(philosophers, makeUrl); undefined urls undefined

janlimpens commented 13 years ago

I must add, that I do not experience this everywhere.

ghost commented 13 years ago

Hmm, your first example works as expected for me...are you including any other frameworks on the page? And which browsers or environments are you experiencing this bug in?

janlimpens commented 13 years ago

That's the problem, there are dozens of other frameworks I have no control over. I very much assume, there is some interference (what else could it be?), but it still strikes me as odd that .map does this and I am pretty certain that is not overwritten by something else. It is loaded relatively at the end together with a well behaving bunch (just a new jquery and two simple plugins).

I currently am developing on chrome/win7, but as far as I can tell, this happens everywhere.

On Thu, Jun 9, 2011 at 9:53 AM, kitgoncharov < reply@reply.github.com>wrote:

Hmm, your first example works as expecd for me...are you including any other frameworks on the page? And which browsers or environments are you experiencing this bug in?

Reply to this email directly or view it on GitHub: https://github.com/documentcloud/underscore/issues/229#comment_1333938

Jan

http://limpens.com

ghost commented 13 years ago

Okay, let's see if we can find the source of the conflict. It sounds as though one of the scripts that you've included on the page is overwriting Array#map. What do you see in the console when you type Array.prototype.map?

jashkenas commented 13 years ago

If Array.prototype.map is being overwritten, then that's not an Underscore.js bug, I'm afraid. Good luck getting it sorted out.

janlimpens commented 13 years ago

@jashkenas, are you sure this is the case or are you assuming only? (did not have time to check kitgoncharov question yet, working on a different project right now...)