Stupid javascript arrays!



  •  Just spent a while trying to figure out why I was getting duplicate items in an array in Javascript... it turns out that you can't really iterate through arrays in Javascript using "for (item in array)", because doing so only iterates through the array object's properties, and not its actual items, so when I checked the array to see if the item already existed, it was never found because the items were never even being iterated over! Aargh!



  • @ekolis said:

    you can't really iterate through arrays in Javascript using "for (item in array)", because doing so only iterates through the array object's properties, and not its actual items
     

    Nope, that's not right.

    for...in will correctly loop over all items in an array. It will ignore its default members, and only include those you've added yourself. For sparse arrays, it will also ignore empty (non-existent) numerical members.



  •  Really? Maybe I created the array wrong then... was I supposed to use "new Array()" instead of just "Array()"?



  • JavaScript doesn't have arrays. It has associative-arrays-that-kind-of-pretend-to-be-arrays-at-least-until-you-insert-something-with-an-out-of-order-index arrays.

    That said I don't get your complaint at all? Maybe a code sample would help?

    Edit:

    @ekolis said:

    Really? Maybe I created the array wrong then... was I supposed to use "new Array()" instead of just "Array()"?

    Oh. Yeah, now I see the problem. If you want a new array, yes, you have to use the "new" operator.



  • @blakeyrat said:

    @ekolis said:
    Really? Maybe I created the array wrong then... was I supposed to use "new Array()" instead of just "Array()"?

    Oh. Yeah, now I see the problem. If you want a new array, yes, you have to use the "new" operator.

    So what happens when you omit the "new" operator?

    I've been bitten by the for..in thing before. It was from someone else's original code, and I was just trying to figure out why it wasn't working. I really haven't worked with javascript very much.



  • @boomzilla said:

    So what happens when you omit the "new" operator?

    Well, here's the thing: if it's your own object, you'd be running the constructor in the global object's namespace. Which means it could very well be stomping over "global" variables without knowing it, which is generally considered a Bad Thing. Especially since the fast-and-loose style of JS doesn't lead to any kind of warnings or errors when it happens. I'm no fan of StackOverflow, but this thread does a good job of discussing that situation.

    With a system object like Array(), I'm not sure what happens, and I'm mildly surprised it doesn't throw an exception. But the point is it doesn't do what you want.

    @boomzilla said:

    I've been bitten by the for..in thing before. It was from someone else's original code, and I was just trying to figure out why it wasn't working. I really haven't worked with javascript very much.

    It's a crazy, awesome, language. Unfortunately, it's usually shackled by DOM, which gives JavaScript the bad reputation that DOM deserves.



  • @blakeyrat said:

    @boomzilla said:
    So what happens when you omit the "new" operator?

    Well, here's the thing: if it's your own object, you'd be running the constructor in the global object's namespace. Which means it could very well be stomping over "global" variables without knowing it, which is generally considered a Bad Thing. Especially since the fast-and-loose style of JS doesn't lead to any kind of warnings or errors when it happens. I'm no fan of StackOverflow, but this thread does a good job of discussing that situation.

    With a system object like Array(), I'm not sure what happens, and I'm mildly surprised it doesn't throw an exception. But the point is it doesn't do what you want.

    Ah, thanks. It seems like a weird side effect of the unintuitive scoping rules of javascript. Or something. Either way, my curiosity is satisfied. I'm not entirely sure if it happened through gaining knowledge or getting more confused.



  • @ekolis said:

    Really? Maybe I created the array wrong then... was I supposed to use "new Array()" instead of just "Array()"?
     

    You come from PHP, right?

    If you want to instantiate a new object from a class, use new. A loose Array(); does nothing at all.

    @boomzilla said:

    the unintuitive scoping rules of javascript
     

    What's unintuitive about it?

    It only gets slightly tricky when you're making classes based on functions and use the this keyword. But that's easily circumvented by always declaring something like var _self = this; as the first line in your classfunction.



  • @dhromed said:

    @ekolis said:

    Really? Maybe I created the array wrong then... was I supposed to use "new Array()" instead of just "Array()"?
     

    You come from PHP, right?

     

    Actually, I come from .NET, where you always use new, but for some reason I thought Javascript was one of those weird languages where you sometimes used new and sometimes didn't, sort of like C...

     



  • @ekolis said:

    Actually, I come from .NET, where you always use new, but for some reason I thought Javascript was one of those weird languages where you sometimes used new and sometimes didn't, sort of like C...

    .NET has multiple languages of course, but both of the ones I'm familiar with... C# and Visual Basic... don't require you to use "new" all the time. So there. Nyah.



  • @blakeyrat said:

    @ekolis said:
    Actually, I come from .NET, where you always use new, but for some reason I thought Javascript was one of those weird languages where you sometimes used new and sometimes didn't, sort of like C...
    .NET has multiple languages of course, but both of the ones I'm familiar with... C# and Visual Basic... don't require you to use "new" all the time. So there. Nyah.

    syntactic sugar



  • @serguey123 said:

    syntactic sugar

    I buy my C# from Mexico so I can get real cane sugar. (Here's the point where some wag comes in and tells me I never make jokes.)

    Look, the point is: in C# you use the "new" operator to instantiate a class and run its constructor. That's the exact same reason you use it in JavaScript, just swap in the words "prototypical object" for class, since JavaScript doesn't have classes. So that should make future JavaScript coding easier, knowing that.

    The "JavaScript is weird" part is that an object's constructor doesn't have to run in the namespace of the object itself. It's just a normal function you can call at any normal time in the normal way. And it running in the global namespace isn't an error or warning or anything. It's just one of those quirks you have to get used to.



  • @blakeyrat said:

    @serguey123 said:
    syntactic sugar
    I buy my C# from Mexico so I can get real cane sugar. (Here's the point where some wag comes in and tells me I never make funny jokes.)

    Look, the point is: in C# you sometimes use the "new" operator to instantiate a class and run its constructor. That's the exact same reason you use it in JavaScript, just swap in the words "prototypical object" for class, since JavaScript doesn't have classes. So that should make future JavaScript coding easier, knowing that.

    The "JavaScript is weird" part is that an object's constructor doesn't have to run in the namespace of the object itself. It's just a normal function you can call at any normal time in the normal way. And it running in the global namespace isn't an error or warning or anything. It's just one of those quirks you have to get used to.

    FTFY

    "new" has more than one use (yeah, I know, pedantic dickweed)



  • I work with javascript aray like this:

    var fruits = ["Banana", "Orange", "Apple", "Mango","Jackfruit", "Coconut"];
    for(var i =0; i < fruits.length; i++)
    {
    document.write(fruits[i] + "
    ");
    }



  • @ekolis said:

    Really? Maybe I created the array wrong then... was I supposed to use "new Array()" instead of just "Array()"?
    An array litteral is the better choice : clearer, shorter, bug-free. Go for var t = [ ];

    @Nagesh said:

    document.write
    Never do that again, Nagesh. (Soul burning in hell, etc.) (and if you think it's cargo cult i'll assume you don't use JS daily)



  • @blakeyrat said:

    JavaScript doesn't have arrays. It has associative-arrays-that-kind-of-pretend-to-be-arrays-at-least-until-you-insert-something-with-an-out-of-order-index arrays.
    With which language's arrays do you compare Javascript's ? What would you consider a decent array implementation ? I'll look lame once again (yay !) but I can't see any major problem with JS arrays... enlighten me, guys.



  • @toshir0 said:

    @Nagesh said:
    document.write
    Never do that again, Nagesh. (Soul burning in hell, etc.) (and if you think it's cargo cult i'll assume you don't use JS daily)

    You can do it as a quick and dirty "here's how to demo traversing an array in JavaScript to post on a web forum", but never put it in production code.

    @toshir0 said:

    @blakeyrat said:
    JavaScript doesn't have arrays. It has associative-arrays-that-kind-of-pretend-to-be-arrays-at-least-until-you-insert-something-with-an-out-of-order-index arrays.
    With which language's arrays do you compare Javascript's ? What would you consider a decent array implementation ? I'll look lame once again (yay !) but I can't see any major problem with JS arrays... enlighten me, guys.

    I didn't say anything was wrong with it. Like everything in JavaScript, it's just different from almost every other language while looking identical at a casual glance. I love JS weirdness.



  • @blakeyrat said:

    I love JS weirdness.
    Weird ? Javascript ? No way.

    alert([ ] == [ ]);// >>> false
    alert(typeof NaN);// >>> "number" (NaN means " not-a-number")
    alert(null > 0);// >>> false
    alert(null == 0);// >>> false
    alert(null >= 0);// >>> true .... WTF ?!

     /facepalm



  • @toshir0 said:

    @ekolis said:

    Really? Maybe I created the array wrong then... was I supposed to use "new Array()" instead of just "Array()"?
    An array litteral is the better choice : clearer, shorter, bug-free. Go for var t = [ ];

    @Nagesh said:

    document.write
    Never do that again, Nagesh. (Soul burning in hell, etc.) (and if you think it's cargo cult i'll assume you don't use JS daily)

    i never using document.write in real producton code. anyway what is story on that?



  • @toshir0 said:

    @blakeyrat said:

    I love JS weirdness.
    Weird ? Javascript ? No way.

    alert([ ] == [ ]);// >>> false
    alert(typeof NaN);// >>> "number" (NaN means " not-a-number")
    alert(null > 0);// >>> false
    alert(null == 0);// >>> false
    alert(null >= 0);// >>> true .... WTF ?!

     /facepalm

     

    Only the last one is a WTF.

    But still what the fuck kind of code are you writing that results in a comparison between null and 0?

     



  • @blakeyrat said:

    @ekolis said:
    Actually, I come from .NET, where you always use new, but for some reason I thought Javascript was one of those weird languages where you sometimes used new and sometimes didn't, sort of like C...
    .NET has multiple languages of course, but both of the ones I'm familiar with... C# and Visual Basic... don't require you to use "new" all the time. So there. Nyah.
    When do you not?  Are you referring to things like Activator?



  • @Sutherlands said:

    @blakeyrat said:

    @ekolis said:
    Actually, I come from .NET, where you always use new, but for some reason I thought Javascript was one of those weird languages where you sometimes used new and sometimes didn't, sort of like C...
    .NET has multiple languages of course, but both of the ones I'm familiar with... C# and Visual Basic... don't require you to use "new" all the time. So there. Nyah.
    When do you not? Are you referring to things like Activator?

    int i = 0; // NO "new" OMG RUN RUN FLEE OMG OMG RUN!



  • @blakeyrat said:

    @Sutherlands said:

    @blakeyrat said:

    @ekolis said:
    Actually, I come from .NET, where you always use new, but for some reason I thought Javascript was one of those weird languages where you sometimes used new and sometimes didn't, sort of like C...
    .NET has multiple languages of course, but both of the ones I'm familiar with... C# and Visual Basic... don't require you to use "new" all the time. So there. Nyah.
    When do you not? Are you referring to things like Activator?

    int i = 0; // NO "new" OMG RUN RUN FLEE OMG OMG RUN!

    Yes of course, but as I was saying, it is syntactic sugar, as a matter of fact int i=new Int32(); is totally valid as well, a tad pointless for the most part of course.



  • @blakeyrat said:

    @Sutherlands said:

    @blakeyrat said:

    @ekolis said:
    Actually, I come from .NET, where you always use new, but for some reason I thought Javascript was one of those weird languages where you sometimes used new and sometimes didn't, sort of like C...
    .NET has multiple languages of course, but both of the ones I'm familiar with... C# and Visual Basic... don't require you to use "new" all the time. So there. Nyah.
    When do you not? Are you referring to things like Activator?

    int i = 0; // NO "new" OMG RUN RUN FLEE OMG OMG RUN!

    As I was saying, syntactic sugar



  • @serguey123 said:

    As I was saying, syntactic sugar

    And the words "syntactic sugar" somehow magically make that line have a "new" in it even though any sane and rational and intelligent person who looked at it would say "hey there's no word 'new' here"?

    Look, I don't want to get into these semantic discussions because:
    1) They're stupid
    2) They're extremely dickweedish
    but I do want to say that if you can look at this line of code "int i = 0;" and somehow see the word "new", then you are delusional. Since I'm not delusional, I'm sticking with my original opinion: that "new" isn't universally required in C#.



  • @blakeyrat said:

    @serguey123 said:
    As I was saying, syntactic sugar
    And the words "syntactic sugar" somehow magically make that line have a "new" in it even though any sane and rational and intelligent person who looked at it would say "hey there's no word 'new' here"?

    Look, I don't want to get into these semantic discussions because:
    1) They're stupid
    2) They're extremely dickweedish
    but I do want to say that if you can look at this line of code "int i = 0;" and somehow see the word "new", then you are delusional. Since I'm not delusional, I'm sticking with my original opinion: that "new" isn't universally required in C#.

    Ohh, I agree with you, the point is that wheter you put new or not there is little difference, the IL is the same, as new calls the default constructor for the struct and does nothing else (guess what happens when you don't put new) so this was left like that for little more than backward compatibility.



  • @serguey123 said:

    Ohh, I agree with you,

    You're delusional and a jackass. Go fuck yourself.



  • @blakeyrat said:

    @serguey123 said:
    Ohh, I agree with you,
    You're delusional and a jackass. Go fuck yourself.

    Care to join me?


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.