-
-
Save nzakas/762069 to your computer and use it in GitHub Desktop.
/* | |
* 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"; |
Good call, that's a typo in the gist, it should be pre-ECMAScript 1. The fact that you're missing is that JavaScript was implemented in Netscape 2, before ECMA-262 existed. There were no arrays in that version.
Sorry, I was overly aggressive in my previous comment. I do remember being aware that JavaScript had once not had Arrays, so that only leaves Netscape 2, which was out of the picture by the time I started JavaScript in 1998.
Was the arguments object available back then? Depending, you could have done something like this no?
var Array0 = function(){
var array = {};
for (var i = 0; i < arguments.length; i++){
array[i] = arguments[i];
}
return array;
}
var new_array = Array0(3,4,5,'wot','okay');
new_array[0]; //3
new_array[1]; //4
new_array[2]; //5
new_array[3]; // 'wot'
new_array[4]; // 'okay'
JavaScript 1.1 introduced the arguments object. JavaScript 1.0 did not have it. I believe the first ECMA standard was based on JavaScript 1.2 that appeared in Netscape 4, but I couldn't swear to it.
lol @timdown...
A couple of years of learning to be slower to judge and generally nicer on the internet makes me a bit shocked at my efforts here. Sorry again, Nicholas.
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.
It is 2023, you can write colors
this way now:
let colors = ["red", "blue", "green", "black"]
This is rubbish. I remember using Arrays in the late 90s in Netscape 3. A little research shows that Arrays were in the first version of the ECMAScript spec released in 1997.