Skip to content

Conversation

@efedin
Copy link

@efedin efedin commented Feb 12, 2011

There is local function each() in Enumerable() which is preferable to use in other functions. The commit provides solving the problem of Array.prototype.each() replacing between including prototype.js and using all(), every(), any(), some(), detect(), include() properties.

Using local function each() to prevent consequenses
of Array.prototype.each() replacing. It happens when
prototype.js is using with mootools.js for example.

Basic situation when there is an error:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/mootools/1.11/mootools.js"></script>
<script type="text/javascript">
var justArray = ["A", "B"];
var elementFromArray = "A";
var check = function(value) {
        return value === elementFromArray;
};
console.log(justArray.some(check));
</script>

This problem arise from
Array.prototype.each = Array.prototype.forEach;
in mootools.js and from using this property in Enumerable: local one handles exceptions,
native does not do it. Thus all(), every(), any(), some(), detect(), include() properties are broken
when they throw exception.

This patch solve the problem by using local each().
@savetheclocktower
Copy link
Collaborator

Sorry for leaving this open for so long.

I'm intentionally choosing not to do this. I want users to be able to redefine each in classes that use Enumerable (should they feel they need to) and have the other Enumerable methods use the redefined version of each. I think to do otherwise would be confusing behavior.

(See also dom.js: Element.toggle contains calls to Element.hide and Element.show, instead of the internal hide and show functions, so that a user can redefine Element.hide and Element.show and have Element.toggle call those redefined methods.)

It's true that this lets another script maliciously (or just accidentally) screw up other Enumerable methods by redefining Array.prototype.each, but any such script would probably conflict with Prototype in a number of additional ways anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants