Home  >  Article  >  Web Front-end  >  How to use JavaScript for loop in multiple browsers_javascript skills

How to use JavaScript for loop in multiple browsers_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:48:501337browse
1. Foreword
The JavaScript language has slight differences in different browsers, but it is not as big as the difference in DOM operations. Now I will list one of the differences in "for loop" for you. And introduce how to effectively resolve this difference.

2. Problem Description
In the following test code example 1, the output results of IE6 and Chrome are inconsistent. IE6 does not execute the code in the for statement
Copy code The code is as follows:

//Example 1:
alert("Prepare to test whether toString is in a for loop Enumerated out")
var forTest = { toString: 1 }
for (i in forTest) {
alert("toString is looped out")//This is not executed under IE6, but Execute in Chrome and output the result value "1"
}

3. Analysis problem
Objects in JavaScript contain 'toString', 'valueOf', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'hasOwnProperty', 'constructor' are seven built-in methods. These seven built-in methods cannot be enumerated using the for statement. But IE6 and Chrome have inconsistent support for built-in method overrides.
IE6: Although its built-in override method can be used, the FOR loop cannot be enumerated.
Chrome: You can override its built-in methods, and the FOR loop can also enumerate the overridden built-in methods.
So the output results of IE6 and Chrome browsers in the above test code example 1 are inconsistent

4. Solve the problem
To solve the problem described above, we need to do two things Things:
Whether the browser used by the user supports FOR loops to enumerate overridden built-in methods
How to elegantly solve the incompatibility problem so that all browsers can FOR loops to enumerate overridden built-in methods
(Solution code example 2)
Copy code The code is as follows:

//Example 2 :
enumerables = true,
forTest = { toString: 1 }
for (i in forTest) {
enumerables = null;
}
if (enumerables) {//These They are all properties of the Object object. The for loop of some browsers (ie6) will not traverse these properties, so you have to manually traverse the properties
enumerables = ['hasOwnProperty', 'valueOf', ' isPrototypeOf', 'propertyIsEnumerable',
'toLocaleString', 'toString', 'constructor'];
}

//If enumerables is null, the browser supports the built-in method of enumeration override, Otherwise, you can only force the built-in method to be copied to the new object as shown in the following code.
/**
* Copy all attributes to the specified object
* @param {Object} object object to be merged
* @param {Object} config source attribute
* @return {Object} return the merged Object
*/
function apply(object, config) {
if (object && config && typeof config === 'object') {
var i, j, k ;
//The normal method of copying objects here
for (i in config) {
object[i] = config[i];
}
//Compatible with multiple browsers Built-in properties can be copied into new objects
if (enumerables) {
for (j = enumerables.length; j--;) {
k = enumerables[j];
if (config.hasOwnProperty(k)) {//Determine whether the object has a specific attribute. This property must be specified as a string. (For example, config.hasOwnProperty("toString"))
object[k] = config[k];
}
}
}
}
return object;
};

Now write some test codes to verify our results (test code example 3)
Copy the code The code is as follows:

//Example 3:
var a={};
for (i in forTest) {
a[i] = forTest[ i];
}
alert(a.toString) //If the copy fails under ie6, you can only enter "native code", not the value we overwrote.
var b=apply({},forTest)
alert(b.toString)//Using the apply function, the values ​​output in IE6 and Chrome are the coverage value we expect "1"

5. Summary
The author guesses that the for statement in IE6 marks those 7 built-in functions into the ignore list, so they cannot be enumerated in for no matter how they are overwritten, and Chrome can intelligently copy the overwritten built-in functions.
Use the apply function in Solution Code Example 2 to solve the problem of for loop inconsistency in multiple browsers.
The author is a rookie and rarely writes blogs. If I express my views incorrectly or make clerical errors, please be willing to ask the experts to correct them.

6. Frequently Asked Questions
Q: Why not first determine whether the browser version is IE6, and then set the corresponding enumeration scheme?
A: My personal point of view is that I am not sure there are so many browsers in the market (there are N browsers for PCs, as well as mobile browsers, and I don’t know what new versions of browsers will be available in the future). What mechanism is used for the for statement? So let’s test the mechanism of the for statement first.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn