2 ms·
So, if initially there were no Array nor Object constructors (there were arrays and objects, but only prepopulated ones, representing anchors, forms, etc, found
by masswerk 6y ago
So, if initially there were no Array nor Object constructors (there were arrays and objects, but only prepopulated ones, representing anchors, forms, etc, found in the document), how were arrays done in a script?
function MyArray() {
this.length = 0;
}
function arrayPush( value ) {
return this[this.length++] = value;
}
function arrayPop() {
return this[--this.length];
}
MyArray.prototype.push = arrayPush;
MyArray.prototype.pop = arrayPop;
var a = new MyArray();
a.push("frist");
// no console available, debug using alert()
alert( a[0] ); // "frist"
alert( a.length ); // 1
alert( a.pop() ); // "frist"
alert( a.length ); // 0
- irrational 6y agoI have been using JS since the mid 90s, but I don't recall needing arrays or objects back then. Frankly we weren't trying to build applications and any real programming work was done on the back end (remember that xmlHttpRequest wouldn't exist for years to come, so any changes to the page were made by going to a new page which would require a round trip to the server, so the server could do anything it needed to do to the page before returning it). The only things I can recall using JS for back in the early days was displaying the date and time at the bottom of the page (why we felt like that was useful is beyond me).
- masswerk 6y agoThere were a few interactive scripts, usually interacting with a form as an UI. The first JS book, I knew, was the "Official Netscape JavaScript Book" (1996), which was based on Netscape 3 beta. This one featured a pretty complex area code application as a demonstration object and things like order forms. I started pretty much with the introduction of NS3, when NS2 and JavaScript1.0 were still the common platform. Fun fact, for a year or so, you'd frequently include a script twice, once in a simpler form for NS2 and similar, and once targeting Netscape 3 (and, later, IE3) using a `<script language="javascript1.1">` tag, which was ignored by the earlier browsers, implementing a more complex version of the same functions. (This scheme was used again, when DHTML became a thing, this time using the `language="javascript1.2"` attribute. However, this switch came with a few caveats, since it also activated some rather odd behavior, like negative zero as a numeric value, which could break a few things.)
- sethammons 6y agowhat a trip, I forgot about displaying the current time and date haha. The other major usage I had for JS back in the day was to focus the cursor on the login box on page load.
- kalleboo 6y agoThe other major uses of JavaScript were hover-over-button effects, and scrolling text in the status bar
- irrational 6y agoOh my. I'd forgotten about scrolling text. Why did we do that? LOL
- mercer 6y agoWhat a magical time... I remember being wowed by having a string of text follow my mouse around.
- sergeykish 6y agoThere was no `prototype` and `constructor` properties o = new Object o.foo = 'bar' a = new Array a[0] = 'baz' But functions were constructors function hello() { return this.name } function Person(name) { this.name = name this.hello = hello } new Person('James') Array push var length = 0 a[length++] = 'foo' a[length++] = 'bar' Actually it was not bad for scripting language, no WAT: new Object + new Array //null is not a number No `typeof` (so no `typeof null`), better `parseInt` parseInt("foo") //0 Much better start than ECMAScript 1
- masswerk 6y ago> There was no `prototype` and `constructor` properties You are correct. The prototype property was yet another JavaSript1.1 (NS3) feature. (Totally forgot about that.) So, ignore '.prototype' in my example. But you could assign properties and methods to objects. Therefor it was more like, var a = new MyArray(); a.pop = arrayPop(); a.push = arrayPush(); > No `typeof` (so no `typeof null`), better `parseInt` Fun fact: also no `isNAN()`. This was, once again, introduced with JS1.1/NS3 and still nonfunctional in IE3 – yes, IE3 had a few of those nonfunctional implementations. (`isNAN()` was actually available in the Unix implementation of Netscape 2, but this was of lesser concern for the public Web.) On the positive side, the access to forms and the various form elements was pretty sound and mature, right from the beginning, since this was what JS was pretty much all about.
- sergeykish 6y agoSorry, NN2 does not agree, it overwrites `length`: function MyArray() { this.length = 0; } var a = new MyArray(); alert(a[0]) // 0 ... alert( a[0] ); // "frist" alert( a.length ); // "frist" alert( a.pop() ); // JavaScript Error: length is not a numeric literal. alert( a.length ); How clever - https://www.javaworld.com/article/2077150/using-javascript-s-built-in-objects.html?page=2 https://www.javaworld.com/article/2077150/using-javascript-s... - all you have to do is count from 1
- masswerk 6y ago