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

```
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
```

FRANKIETANG
浏览 2472回答 1
1回答

橋本奈奈未

不是很懂为什么要自己实现一个。Array.prototype._forEach = function(fn){     for(var i=0; i<this.length; i++){ fn(this[i], i, this);     } }

慕瓜9220888

PUFINQUHNXLUTPSYLSFDCNTBRORMQPEQDGNDEFNQLFIPIIPBHTWJAQGOTWYBWTGNPVLCPQUXZUSRXBLHORKNDJXAOVPYODTRWAOFXNCPSNHYPUMIYVZTAIPSEHSVJQFHDCIJJNRFBKTPDGRPPRQWFJKXDNFBBARVFVKJPYSCCSPFTZZHHVZJYZPKDZDZDIQMPOFEAGWBQOOOFGEJAMIDAUXKQMOEQTJGGDVQMYRLORLPPYHQUHXUKGOQROIVACBXMZJFSYNITOLFLYZDVRNKYIFUSUDATTQJXFHGVYROFFDHJMQKOMDCIXDHVPMONMIOYMYMWETPFDDRMODQTZCWQMKOLHNAUIZKMLVZMGKIVXFSXYFPGKVBKAOLFDXPVJPTGRVXPEUUJXECTBYCOYSQBQSYQQKOCJDWKTPEZQAYKGCYWZXOTOXHQAEYWOPMBRLZWQWAPTSPSFJOEUQSQDQUUWJOERADAECLACIPMGZEWSKBKHUQBRHGKOLMSNHNQUOLFWLANDXJWKWEKBHVPJXHCWLRVFJTPAYUQKVSPQSONNDDRBEPVUHBDBSQLHXWKOTXGHUKZKEROWYXXTKNRPJEIHTKELZRCEXPCMSYKLYDJCPMZLTBDPFZSLYTCRXTNQTFDCYBIOQTGOTNFIFSOJOEACVPMSEWRNJWWBYJKFHDWTNSTXGBXXKKKHMPTIEELXMXTQMJIIVUJSHAQDDIYOHEXBFSDFURVHLPJGJAMFVPDQKEQWVECWJHGSIEQUOYDFRA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