΢ Ͷ ^ ھ

cba赛程转播 :jsԭmapFķY

cba㶫 www.axwwg.com  •rg2020-01-26 01:01:26   ߣ   ҪuՓ(0)

jsԭmapF




<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-sca

jsԭmapF

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="author" content="">
  <title>map</title>
</head>

<body>

  <script>
    Array.prototype.my_map = function (callback) {
      if (!Array.isArray(this) || !this.length || typeof callback !== 'function') {
        return []
      } else {
        let result = [];
        for (let index = 0; index < this.length; index++) {
          const element = this[index];
          result.push(callback(element, index, this))
        }
        return result
      }
    }

    let arr = [1, 2, 3, 4, 5]
    let res = arr.my_map((ele, i) => {
      return ele + 10
    })
    console.log(res)
  </script>
</body>

</html>

a֪Rc

҂ƽrõѽbõmap׌҂ԼbһmapԓΌF

f׃x䌍vMĺ߀forѭhbһmap

˼·

1.ԭһ
2.һthis
3.call ąͷbõmapąһӵ

Array.prototype.fakeMap = function(fn,context) {
	let arr = this;
	let temp = [];
	for(let i=0;i<arr.length;i++){
		let result = fn.call(context,arr[i],i,arr);
		temp.push(result);
	}
	return temp;
}

ϾDZνBȫP֪Rcκa“ϵ_֮ҵС

ܸdȤ:

  • ԭJS forEach()map()vą^e݌jQuery $.each$.mapv

P

  • jsԭmapFķY

    jsԭmapFķY

    jsԭmapF <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-sca
    2020-01-26