Re:The Universal Data Structure
by dhimes on Tuesday June 30, 2015 @02:22PM
(#9807365)
"And we can’t forget our favorite JavaScript interview question of all time: If you only had twenty-four hours to implement arrays in JavaScript, how would you do it?"
[ Parent
| on HN ]
Aren't they just objects that have integer properties; that's why you can
for (k in ['a', 'b']) {
console.log(k);
}
and get back 0, 1? However: var a = {0:'a', 1:'b'};
for (k in a) {
console.log(k);
}
also outputs 0, 1.Edit: turns out, you can even have doubles, too:
var weird = {3.14:'hello', 6.28:'world'};
// for loop above emits: 3.14, 6.28
console.log(weird[3.14]); // emits 'hello'
Re:The Universal Data Structure
by chriswarbo on Tuesday June 30, 2015 @04:44PM
(#9808313)
> Edit: turns out, you can even have doubles, too:
That's because Javascript doesn't actually have integers, it just has "Number":
> The Number type has exactly 18437736874454810627 (that is, 264−253+3) values, representing the double-precision 64-bit format IEEE 754 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic
Re:The Universal Data Structure
by tomjakubowski on Tuesday June 30, 2015 @04:38PM
(#9808284)
JavaScript objects can only have string keys (this may have changed in ES2015 with Symbols). If you pass a non-string value in an object literal or inside a []-accessor, that value is converted to a string.
I'm not really versed on JavaScript. I presume this is a joke about the lack of a proper array structure in JavaScript? How would one answer this question anyway?
This is probably intended to be a joke about how Javascript was originally developed over about 10 days, which has been the root of many of JS's problems.
http://www.computer.org/csdl/mags/co/2012/02/mco2012020007.p...
But this heritage produced, years later, a great presentation about the evolution of JavaScript... that was best viewed without JavaScript.
While it's probably joke, JavaScript's Array is fine (although it's more like a 'vector'). JavaScript VMs implement it as a real array (in fact, plain objects are often optimized into arrays too).