Let IE and the old W3C browser also support several new methods of es5 array objects.

Source: Internet
Author: User

A simple comment is written. For more information, see the es5 manual.

// Simulate es5 array. Prototype. foreach
If (! Array. Prototype. foreach ){
Array. Prototype. foreach = function (F, othis ){
If (! F | f. constructor! = Function. Tostring ()) Return;
Othis = othis | window;
For (VAR I = 0, Len = This. length; I <Len; I ++ ){
F. Call (othis, this [I], I, this); // P1 Context Environment P2 array element P3 index P4 array object
}
}
}

// Simulate es5 array. Prototype. Filter
If (! Array. Prototype. Filter ){
Array. Prototype. Filter = function (F, othis ){
If (! F | f. constructor! = Function . Tostring () ) Return;
Othis = othis | window;
VaR A = [];
For (VAR I = 0, Len = This. length; I <Len; I ++ ){
If (F. Call (othis, this [I], I, this) A. Push (this [I]);
}
Return;
}
}

// Simulate es5 array. Prototype. Map
If (! Array. Prototype. Map ){
Array. Prototype. Map = function (F, othis ){
If (! F | f. constructor! = Function . Tostring () ) Return;
Othis = othis | window;
VaR A = [];
For (VAR I = 0, Len = This. length; I <Len; I ++ ){
A. Push (F. Call (othis, this [I], I, this ));
}
Return;
}
}

// simulate es5 array. Prototype. Every
If (! Array. Prototype. Every) {
array. Prototype. Every = function (F, othis) {
If (! F | f. constructor! = Function . tostring () ) return;
othis = othis | window;
for (VAR I = 0, Len = This. length; I If (! F. call (othis, this [I], I, this) return false;
}< br> return true;
}< BR >}

// simulate es5 array. Prototype. Some
If (! Array. Prototype. Some) {
array. Prototype. Some = function (F, othis) {
If (! F | f. constructor! = Function . tostring () ) return;
othis = othis | window;
for (VAR I = 0, Len = This. length; I If (F. call (othis, this [I], I, this) return true;
}< br> return false;
}< BR >}

 

The indexof lastindexof method is a simple version, but a small problem is fixed.Code

// Simulate the es5 array. Prototype. indexof method. Fix the issue that the value type of the browser that implements the indexof method, such as ff, returns false if the value type of the browser is equal to the value of the reference type.
Array. Prototype. indexof = function (OBJ ){
For (VAR I = 0, Len = This. length; I <Len; I ++ ){
If (compare (this [I], OBJ) return I;
}
Return-1;
}
// Simulate the es5 array. Prototype. lastindexof method. Fix the issue that if the value type of the browser that implements the indexof method such as FF is equal to that of the reference type, false is returned.
Array. Prototype. lastindexof = function (OBJ ){
For (VAR I = This. Length-1; I> = 0; I --){
If (compare (this [I], OBJ) return I;
}
Return-1;
}

Yes. The annotations have been written clearly. The reason why I use this method to overwrite es5 is that


// Compare whether the object is equal to the OBJ parameter ..
Function compare (obj1, obj2 ){
If (obj1 = NULL | obj2 = NULL) Return (obj1 = obj2 );
Return (obj1 = obj2 & obj1.constructor. tostring () = obj2.constructor );
}

This compare method. What problems does it solve? Right! It is equality judgment, because I found that the equality judgment of the original version will actually think

1! = New number (1) indicates that if a number object exists in the array and I use number to compare the objects directly, even if they are equal, false is returned.

In this case, when JavaScript references the value type to perform equality operations, it will call the valueof method of the reference type, that is, the object, and then compare it.

It indicates that JavaScript 1.6 uses the === to strictly determine the equality when implementing the indexof method...

Then, the compare method I wrote solves this problem and allows equality judgment to follow the original JavaScript rules...

When determining that an object is of a specific type, foreigners must use object. porototyp. tostring. Call (this) for comparison to prevent

For example, [1, 2, 3]. constructor! = IFRAME. contentWindow. array...

In fact, this is not so troublesome. We only need to call one of the constructors on both sides to return a value-type string pseudo object.

In this way, the reference type on the other side will automatically call the valueof method to compare strings. Why bother?

 

Finally, I like foreach map filter and other methods very much in general. It works very well. Since several other methods are similar, I wrote them together!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.