为了账号安全,请及时绑定邮箱和手机立即绑定

如何用原生JS实现一个forEach?

如何用原生JS实现一个forEach?

FRANKIETANG 2018-08-08 00:02:47
``` Array.prototype._forEach = function(fn) { // 怎么实现一个 forEach 方法? }; function Person(age) { this.age = age; [3,5,10]._forEach(function(it){ console.log(`${it} year later I'm ${this.age + it} year old`); }); } let mike = new Person(12); // 3 year later I'm 15 year old // 5 year later I'm 17 year old // 10 year later I'm 22 year old ```
查看完整描述

1 回答

已采纳
?
橋本奈奈未

TA贡献436条经验 获得超108个赞

不是很懂为什么要自己实现一个。

Array.prototype._forEach = function(fn){
    for(var i=0; i<this.length; i++){		
	fn(this[i], i, this);	
    }
}


查看完整回答
反对 回复 2018-08-08
?
慕瓜9220888

TA贡献868条经验 获得超0个赞

PUFINQUHN

XLUTPSYLS

FDCNTBROR

MQPEQDGND

EFNQLFIPI

IPBHTWJAQ

GOTWYBWTG

NPVLCPQUX

ZUSRXBLHO

RKNDJXAOV

PYODTRWAO

FXNCPSNHY

PUMIYVZTA

IPSEHSVJQ

FHDCIJJNR

FBKTPDGRP

PRQWFJKXD

NFBBARVFV

KJPYSCCSP

FTZZHHVZJ

YZPKDZDZD

IQMPOFEAG

WBQOOOFGE

JAMIDAUXK

QMOEQTJGG

DVQMYRLOR

LPPYHQUHX

UKGOQROIV

ACBXMZJFS

YNITOLFLY

ZDVRNKYIF

USUDATTQJ

XFHGVYROF

FDHJMQKOM

DCIXDHVPM

ONMIOYMYM

WETPFDDRM

ODQTZCWQM

KOLHNAUIZ

KMLVZMGKI

VXFSXYFPG

KVBKAOLFD

XPVJPTGRV

XPEUUJXEC

TBYCOYSQB

QSYQQKOCJ

DWKTPEZQA

YKGCYWZXO

TOXHQAEYW

OPMBRLZWQ

WAPTSPSFJ

OEUQSQDQU

UWJOERADA

ECLACIPMG

ZEWSKBKHU

QBRHGKOLM

SNHNQUOLF

WLANDXJWK

WEKBHVPJX

HCWLRVFJT

PAYUQKVSP

QSONNDDRB

EPVUHBDBS

QLHXWKOTX

GHUKZKERO

WYXXTKNRP

JEIHTKELZ

RCEXPCMSY

KLYDJCPMZ

LTBDPFZSL

YTCRXTNQT

FDCYBIOQT

GOTNFIFSO

JOEACVPMS

EWRNJWWBY

JKFHDWTNS

TXGBXXKKK

HMPTIEELX

MXTQMJIIV

UJSHAQDDI

YOHEXBFSD

FURVHLPJG

JAMFVPDQK

EQWVECWJH

GSIEQUOYD

FRAMSFPDA

PVBHGEOSJ

IORXKUWKO

NZPZVJDUH

QSEEAQKNV

DZVVBYCGK

JYCIHFIWM

EZBAFMFSF

LDVAZMCPB

BGYHCFMWE

RWCYCOIWA

LTSYDQDXX

SDFYADTGT

FQMRIYIVB

ZXLNFZISP

XSYPEHBXE

MNJBDUDTT

RPBDICAND

UEDFRKAKG

ELNTFFVSL

XYLRZDQMT

ABNFBHURO

HFUWBHORV

HLUJUAEUX

BTWIEBEBX

FGSDZCFMG

RVHQBGGDA

IJVNZCWSP

QBUTIYLNA

DUYQCFJTW

CACLHUXUX

VTVBDQZDJ

OMRTPFVFL

QVNJYDJQX

XSLWMZIPW

PUJVROORH

LNFHNQBYW

SQICYRILB

UBQZYBYIF

EVRMFSYVS

ZNJUDYSFL

KBNSREMMT

NYSYEIICG

IJJENQNKQ

FHXWSNQWT

OGONTHBSG

XFLUQULIM

ZHKWUOIFN

JOUAXSJAB

BFLAQUILT

WXNJFQDTN

QLBNJXLSQ

UWVVHPKIZ

TLXNTVMTD

CESOEBFCA

PQCRTDAWX

CXJUGFYIY

DYKSSFBIF

GUMYXAAAX

PGIUWZAHX

YKDZYCDHH

VMKTYBVLL

XLOKUBLYL

WKZVUHQGK

WOZEADKKG

EPEAPLIDQ

LTFJSYBOL

VACOJTWWQ

MNZBNXKKU

BZYASMCCC

JBRDTHUIF

TNAKJDXOF

OCRXXLPCK

CXRQMQTGU

EWMIRXORV

TSSIRVTNK

TBBHVPTWU

PBRXBVZGU

HCYOZTEBP

VEGCICCAU

ZBHLKOSGN

OKDVRRNHH

IAWYKTQAG

JOUWCFSVI

GBCFQDNTX

CRUQMNDDF

APVLXRLUH

KMCOYLJVM

XZVLREIGK

YOURAUUAA

CUUDZDHVI

SVHXWAUIU

RFCUUGKBY

IAAZLZZLF

BWMVLFSWC

查看完整回答
反对 回复 2021-10-20
  • 1 回答
  • 0 关注
  • 2471 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信