JSONx is Sexy LIESx
-
I said that was stupid and wrong?
Well, I'm not sure if I agree with your reason for it being stupid and wrong, but it's sure stupid and wrong in any case. It's JS, what did you expect?
-
is going on here?
accalia@SockBotDev:~/SockBot$ node > [,,] [ , ] > [,,].length 2 > JSON.stringify([,,]) '[null,null]' > [,,].forEach(function(){console.log(1);}) undefined >
i know why stringify reported nulls (undefined is coalesced to null for json) but why is that only two elements? and why does forEach not fire correctly for it?
i'm inclined to think we found a bug in V8.
-
ok. i figured out why that's only two elements. V8 ignores trailing commas on the assumption that they are in error.
so you want to do
[,,,]
for three undefineds. But that still doesn't explain why forEach has a problem with it.
-
i'm inclined to think we found a bug in V8.
Replicated exactly in the three browsers I've got convenient access to. Does Firefox use V8?
-
-
Does Firefox use V8?
no, but i'm assuming the behavior of ignoring trailing commas is pretty widespread. as i recall IE8- were the ones that had issue with it.
does the forEach issue also happen on the browsers you tested?
-
no, but i'm assuming the behavior of ignoring trailing commas is pretty widespread.
I thought we talked about that before somewhere. Isn't it a convenience thing?
-
does the forEach issue also happen on the browsers you tested?
That was what I really tested, yes. (The trailing comma thing looks like it might be due to some nuance of the language spec; I didn't bother to check that thoroughly as it doesn't feel much like a bug.)
-
That was what I really tested, yes.
hmm.....
/me wanders off to take a look at the spec and reference implementation for forEach
-
no, but i'm assuming the behavior of ignoring trailing commas is pretty widespread
My research indicates SpiderMonkey (the FF JS engine) will throw a syntax error if it sees a trailing comma
-
the spec
Let kPresent be the result of calling the [[HasProperty]] internal method of O with argument Pk.
If kPresent is true, then
[...]I guess that those elements must be being considered to be not present.
-
that appears to be the case...
BELGIUM!
-
i know why stringify reported nulls (undefined is coalesced to null for json) but why is that only two elements?
V8 ignores trailing commas in lists. I haven't worked in JS in years, and even I knew that. Feh!
IE's JScript does not though. At least, it didn't used to a few years ago.
-
Isn't it a convenience thing?
Yes, it's so when you
have a list,
like this,
you can add,
and remove,
or just re-order,
easily by just,
copying entire lines,
at a time,
and not having to worry,
about the last comma causing,
a syntax error,
-
My research indicates SpiderMonkey (the FF JS engine) will throw a syntax error if it sees a trailing comma
Lies!Well, for arrays anyway.
-
V8 ignores trailing commas in lists.
yeah i figured it out. I've been writing compatible JS for so long i've forgotten which browsers had an issue with the trailing comma and which are fine with it.
still puzzling why forEach fails. I mean i think i know why now but the reason for it seems like a weird edge case that should be patched.
-
-
still puzzling why forEach fails. I mean i think i know why now but the reason for it seems like a weird edge case that should be patched.
This disturbs me:> [undefined, ,undefined].forEach(function(x, idx){console.log(idx);}) 0 2 undefined
-
Well I'd hope so, after like 5 people fucking told you.
Hey, don't be mad fro, she beat your over/under.
-
Well I'd hope so, after like 5 people fucking told you.
you mean like i described in the post immediately after i asked the question, before anyone else had told me?
http://what.thedailywtf.com/t/jsonx-is-sexy-liesx/5317/303?u=accalia
-
you mean like i described in the post immediately after i asked the question, before anyone else had told me?
The problem is you had to ask in the first place, when you work in Node like all the fucking time. I'm sure your code is Super High Quality(tm) if you don't even know the basic syntax of your chosen programming language.
-
Based on the buttumption that Node.js and V8 both implement JS exactly the same way?
Pretty sure they don't…
-
Based on the buttumption that Node.js and V8 both implement JS exactly the same way?
Node.js uses V8! You're even dumber than Accalia!
Goddamned, you people are so stupid you're making me angry, I got a bus to catch.
-
-
you mean when i never leave the trailing commas in the first place, and in fact have my lint tool configured to throw an error when encountering one because historical implementations of JS have had an issue with the trailing comma so I never run into a situation where the comma would be there in the first place?
Is it a sign of bad coding when i don't know the behaviour of a language construct that behaves differently in different interpreters? Wouldn't that be a sign of good coding as I would write more portable code that way?
-
you mean when i never leave the trailing commas in the first place, and in fact have my lint tool configured to throw an error when encountering one because historical implementations of JS have had an issue with the trailing comma so I never run into a situation where the comma would be there in the first place?
Correct. In fact that makes you even dumber. The dumb level is blowing off the roof. The roof of knowledge.
Is it a sign of bad coding when i don't know the behaviour of a language construct that behaves differently in different interpreters?
Indeed. It's also a sign of bad coding to use Node.js in the first place, or to use a dumb fox in a wedding dress as an avatar.
Wouldn't that be a sign of good coding as I would write more portable code that way?
Possibly; but I'd still insult you.
-
Indeed. It's also a sign of bad coding to use Node.js in the first place, or to use a dumb fox in a wedding dress as an avatar.
But… I chose that avatar…
OK, it's not as good as the one she chose for me, and she did change the background (the old one was crap), but still…
-
Correct. In fact that makes you even dumber.
Possibly
are you saying that it is both smart and dumb of me?
but I'd still insult you.
ooooh. you're trying to insult me. Got it.
Good luck, and carry on.
-
are you saying that it is both smart and dumb of me?
Are you expecting him to employ logic and sense?
-
This disturbs me:
◄ [undefined, ,undefined].forEach(function(x, idx){console.log(idx);}) ■ 0 ■ 2 ► undefined ```</blockquote> I am confuzzled at your disturbance, as it seems pretty clear what's going on. You've got an array of length 3, with entries 0 and 2 explicilty set to `undefined`, and entry 1 not defined. You then iterate through the array, printing the index of both of the explicitly-set entries, which doesn't return anything. That evaluating the first entry produces `undefined`, as does the third, as does the *second*, isn't relevant to which entries are actually set.
-
Just because I understand the rationale, doesn't mean I have to like it.
-
In addition to
null
andundefined
, Javascript has a third mysteryvalue_not_found
state which the cell of an Array can take. What's not to like?
-
In addition to
null
andundefined
, Javascript has a third mysteryvalue_not_found
state which the cell of an Array can take
*twitch*
…why?
-
it doesn't really. there's just a cell that has been specified as undefined and a cell that is implicitly undefined.
-
Still doesn't sit with me very well; why have two different versions of
undefined
?
-
Because Javascript
-
i finally figured it out. there isn't.
the 0 and 1 keys aren't defined when creating
[,,]
so forEach ignored them
-
So they're not defined to be
undefined
, implying that they're undefined, but notundefined
, whereas in the other case, they were defined to beundefined
. So you need to be sure whether yourundefined
is a definedundefined
or an undefinedundefined
. Makes sense?
-
-
-
Javascript has a third mystery value_not_found state which the cell of an Array can take.
It's because JavaScript arrays are not like C arrays: they are hashmaps with the convenience length property. So if you don't define the key it is not defined. But "not defined" is not the same as "undefined"
-
This entire thread started with @VaelynPhi lamenting that JSON didn't bake in a JavaScript array quirk. Three hundred replies later, we now have confirmation that JavaScript arrays really are weird.
Many of us knew this a long time ago, including the creator of JSON, who wisely didn't bake the weirdness into JSON.
So, as long as you don't depend on an array element being undefined as being distinct from that element being missing, this will never affect you.
-
I sense closure, and I feel like we all learned something from this topic.
-
I learned to hit the "mute topic" button much earlier.
-
And yet here you are...
-
Is it a sign of bad coding when i don't know the behaviour of a language construct that behaves differently in different interpreters?
Yes.
Blakey has a point, knowingly or not. Even if you're to be believed that "you only write good, clean, linted code", it doesn't mean you can get by without knowing how the bad code works. There are other devs around, and seeing how JS is ripe for abuse, you'll probably see an awful lot of such code.
You can either throw a hissy fit and say "WTF, I don't know how it works, it's bad code and it shouldn't exist, waaah", spend a couple of days refactoring and linting, or just clench your teeth and deal with it. If you don't know the dark corners of the language, you're pretty much reduced to the first option.
-
Why do they have a value named undefined that is distinct from null if they also have the ability to not define a value?
-
Why do they have a value named undefined that is distinct from null if they also have the ability to not define a value?
Because they were not really sure what they were doing when they first implemented that particular idiocy, and now they're stuck with it.
-
I think I see where you are coming from there, but I am still confused.
I knew that the talking comma construct was problematic for browser compatibility. So I don't use it at all and was unaware of an additional quirk of the construct.
So I asked a question about it, and got pounced on for not knowing a piece of trivia about the language that was already firmly in 'here be dragons' country.
Why is that? Why did I have to do all the research to find the answer? Why did it become about the fact that I was not agoddess of the code for knowing everything about my chosen language instead of being about the question I asked to learn more about the language?
-
Well, @blakeyrat pounced on you because he's a dick. But you knew that already ;)
And I don't think @Maciejasjmj is pouncing on you; at worst, he's just being a little blunt.
It seems that @blakeyrat believes that you're a bad developer unless you know every little nuance of your chosen technology. Of course, by that definition, everyone is a bad developer. Including @blakeyrat, since he, like the rest of us, doesn't know every nuance of the tech he uses.
AFAIAC, you don't need to know exactly how a dodgy construct behaves in every possible scenario, only that it is dodgy and should be avoided.