Drongo_III Posted January 14, 2016 Share Posted January 14, 2016 I have a really basic question. Why is it you can't create a multidimensional array like this in javascript: t['key1']['key2'] = 123; I have worked with JS for a long time and I get the proper form is to do something like: t['key1'] = {'key2' : 123} But I'm just wondering why it's not possible to do the first version? Is it because JS doesn't know what to cast the 'key2' to? Thanks, Drongo Quote Link to comment https://forums.phpfreaks.com/topic/300331-js-basic-question/ Share on other sites More sharing options...
Psycho Posted January 14, 2016 Share Posted January 14, 2016 Because, that was how the language was built. Quote Link to comment https://forums.phpfreaks.com/topic/300331-js-basic-question/#findComment-1529665 Share on other sites More sharing options...
Solution Jacques1 Posted January 14, 2016 Solution Share Posted January 14, 2016 This array feature which you appearently know from PHP is actually very exotic and really only makes sense in the super-sloppy PHP philosophy. Technically speaking, you simply cannot apply the index operator to the empty value t['key1']. There's no such thing as null['key2']. If you try this in Ruby, your script will blow up with an error message. The same is true for Python. The only reason why PHP doesn't blow up is because it's specifically designed to accept errors. Instead of telling you to fix your code, it will guess what you mean and create a new subarray for you. So this isn't how languages normally works. It's a PHPism. Quote Link to comment https://forums.phpfreaks.com/topic/300331-js-basic-question/#findComment-1529668 Share on other sites More sharing options...
Drongo_III Posted January 17, 2016 Author Share Posted January 17, 2016 This array feature which you appearently know from PHP is actually very exotic and really only makes sense in the super-sloppy PHP philosophy. Technically speaking, you simply cannot apply the index operator to the empty value t['key1']. There's no such thing as null['key2']. If you try this in Ruby, your script will blow up with an error message. The same is true for Python. The only reason why PHP doesn't blow up is because it's specifically designed to accept errors. Instead of telling you to fix your code, it will guess what you mean and create a new subarray for you. So this isn't how languages normally works. It's a PHPism. Thanks Jacques. You're quite right, I'm a php developer first and didn't realise this was a feature particular to php. Makes much more sense now. Drongo Quote Link to comment https://forums.phpfreaks.com/topic/300331-js-basic-question/#findComment-1529905 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.