Aikido, programming, system administration, and other things I find interesting

A gotcha with javascript objects

Matija

November 23, 2015

Uncategorized

No Comment

Spent some time today figuring this one. I got too used to perl auto-vivifying objects (or hashes – same thing in Perl5).

It seems javascript does the opposite: if a member of of a structure is a string, and then you assign to it as if to an object, it will simply IGNORE your assignment. No error, no change in the assigned object, nothing.

Look at this code (or better yet, execute it in a debugger, step by step).


$(document).ready(function() {
debugger;
var a = new Object;
var b = new Object;
a['b'] = b;
alert(JSON.stringify(a));
b['c'] = 'c';
alert(JSON.stringify(a));
a['b'] = JSON.stringify(b);
alert(JSON.stringify(a));
d=a['b'];
d['c']='d';
alert(JSON.stringify(a));
});

I know, of course that some languages are more strongly typed than others. But I never thought that I would find a case where it straight up ignored an assignment, without generating an error.

Related Posts

Munin-async 2.0.17 on Ubuntu 12.04

Matija

October 26, 2013

Uncategorized

1 Comment

Munin authors have moved on in their development and if you are going to be installing munin-async on any system, I strongly recommend that you use the latest version (2.0.17 at the time of writing), which fixes a number of problems I described in a previous article on Installing munin-async. The only problem I had […]

Read More

Hello ripe64!

Ripe64 is starting in a few hours. I am a member of Go6.si expert council, which is the local organizer of this RIPE meeting. The RIPE set up team is composed of amazing professionals, who obviously have a lot of experience setting up events like this, the setup went smoothly despite some unforseeable mishaps, like […]

Read More

Leave a Reply

Your email address will not be published. Required fields are marked *