321481 (1) [Avatar] Offline
Hi Guys,

// Love the book!

I'm wondering why the 'delete' statements are needed.

The codes above are already testing whether the dependent key exist. The 'else' is run only when the key does not exist, then why delete the the non-exist key...?

//------------------ Code ----------------------------
changeAnchorPart = function( arg_map ) {

for ( key_name in arg_map ) {
// update matching dependent key
key_name_dep = '_' + key_name;
if ( arg_map[key_name_dep] ) {
anchor_map_revise[key_name_dep] = arg_map[key_name_dep];
else {
delete anchor_map_revise[key_name_dep];
delete anchor_map_revise['_s' + key_name_dep];

//-----------------Code End-----------------------

Thank you!
michael.mikowski (247) [Avatar] Offline
First, we must remember that we are *updating* a map that is populated from the existing URI anchor - see copyAnchorMap(), so it is important to add, update, and delete keys according to the arguments.

The if clause does *not* check to see if a key exists - if we wanted to do that, we would use "if ( arg_map.hasOwnProperty( key_name_dep ) )..." Instead, it checks to see if the proposed *value* for the key is "truthy" - like "1" or "foo" - and if it is it retains the key and updates the value.

However, if the value is falsey - like "0" or "" or undef or null - then the key is removed from the map, with the expectation that the consumer of the map will understand why the key was removed.