Home >Web Front-end >HTML Tutorial >Explain the meaning of the statement_html/css_WEB-ITnose

Explain the meaning of the statement_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 12:11:36933browse

Array.prototype.i = Array.prototype.indexOf ||
function(value){
for (var i = this.length; i-- && this[i]!== value;) {}
return i;
};
What does this passage mean? ? Please explain.
Especially
Array.prototype.indexOf ||
function(value)
I don’t understand this!


Reply to the discussion (solution)

The indexOf in Array.prototype.indexOf is the method implemented by the class Array
which means:
If this method is implemented, this Array.prototype.i = Array.prototype.indexOf
That is, i and indexOf agree
If this method is not implemented, use the following function for the extension method i in Array Implement

Array.prototype.i = Array.prototype.indexOf || function(value) {    for (var i = this.length; i-- && this[i] !== value;) {}  //这里有点问题, return i;语句应该是在这个for循环体内的    return i;};//等价于if (Array.prototype.indexOf != false) Array.prototype.i;else Array.prototype.i = function(value) {	for (var i = this.length; i-- && this[i] !== value;) {		return i; 	}}


The function of the code is to add an i() method to the array type, check if the browser itself supports the indexOf method, and i will refer to the method (i becomes an alias of indexOf , if it is not supported, customize an i() method.
Array.indexOf() is supported by JS version 1.6, so it does not mean that all browsers support

such as
. b assignment, if a has a value, then assign the value of a to b, if a has no value, then assign 4 to b

			var a;			var b = a || 4;			alert(b)

is equivalent to
var a;
var b = a ? a : 4;
is equivalent to
var a;
if( a ){
b = a;
}else{
b = 4;
}

JScript code

Array.prototype.i = Array.prototype.indexOf || function(value) {
for (var i = this.length; i-- && ; this[i] !== value;) {} //There is a problem here, the return i; statement should be in the body of this for loop
return i...

like this , thank you all!

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