Created
January 1, 2011 22:51
-
-
Save nzakas/762069 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /* | |
| * Pre-ECMAScript 1, JavaScript didn't have native arrays. | |
| * Those who needed them frequently used functions such as the following. | |
| * Also note that there was no special "undefined" value. | |
| */ | |
| function makeArray(len){ | |
| var array = new Object(); | |
| array.length = len; | |
| //initialize each item with null (empty placeholder) | |
| for (var i=0; i < len; i++){ | |
| array[i] = null; | |
| } | |
| return array; | |
| } | |
| var colors = makeArray(4); | |
| colors[0] = "red"; | |
| colors[1] = "blue"; | |
| colors[2] = "green"; | |
| colors[3] = "black"; |
It is 2023, you can write colors this way now:
let colors = ["red", "blue", "green", "black"]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nicholas, thank you for this! Just started coding in JavaScript and this piece of code really helped me solve my problem I was having with creating multiple arrays with a function.