Created
July 9, 2014 17:09
-
-
Save noel9999/837913f065351b04c805 to your computer and use it in GitHub Desktop.
Javascript:練習實作自己的foreach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.prototype.myEach = function(callback){ | |
for(i=0;i<this.length;i++){ | |
callback(this[i]); | |
} | |
};//這邊是先定義myEach並且透過prototype指定給Array,讓之後所有的Array都可以用此方法 | |
[1,2,3,4,5].myEach(function(element){ | |
console.log(element); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment