5 ms·
There was no `prototype` and `constructor` properties o = new Object o.foo = 'bar' a = new Array a[0] = 'baz' But functions were constructors
by sergeykish 6y ago
There 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 agoYou are correct, once again, all properties were available both by property name (if used in an associative context) and by numeric index. Since we assigned 'length' already in the constructor, we would have to accommodate for this, i.e., start counting at 1. Turns out, my recollections are a bit on the rosy side… :-) Edit: This should work: function MyArray() { this.length = 0; } function arrayPop() { return this[this.length--]; } function arrayPush( value ) { return this[++this.length] = value; } var a = new MyArray(); a.pop = arrayPop; a.push = arrayPush; a.push('a'); a.push('b'); alert( a[1] ); // 'a' for (var i = 1; i <= a.length; i++) alert( a[i] ); at which point it is much simpler to do: function MyArray() {}; var a = new MyArray(), aLength = 0; a[aLength++] = 'a'; a[aLength++] = 'b'; for (var i = 0; i < aLength; i++) alert( a[i] ); var b = new MyArray(), bLength = 0; // etc which is also, what was actually used.