最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Unclear Javascript snippet - Stack Overflow

programmeradmin1浏览0评论

This Javascript MD5 implementation has me confused.

In the global space, the author declares a var:

var hexcase = 0; 

Later on, the following method appears:

function rstr2hex(input)
{
  try { hexcase } catch(e) { hexcase=0; }
  var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  var output = "";
  var x;
  for(var i = 0; i < input.length; i++)
  {
    x = input.charCodeAt(i);
    output += hex_tab.charAt((x >>> 4) & 0x0F)
           +  hex_tab.charAt( x        & 0x0F);
  }
  return output;
}

The line that I don't understand is:

try { hexcase } catch(e) { hexcase=0; }

What is the author trying to acplish here?

This Javascript MD5 implementation has me confused.

In the global space, the author declares a var:

var hexcase = 0; 

Later on, the following method appears:

function rstr2hex(input)
{
  try { hexcase } catch(e) { hexcase=0; }
  var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  var output = "";
  var x;
  for(var i = 0; i < input.length; i++)
  {
    x = input.charCodeAt(i);
    output += hex_tab.charAt((x >>> 4) & 0x0F)
           +  hex_tab.charAt( x        & 0x0F);
  }
  return output;
}

The line that I don't understand is:

try { hexcase } catch(e) { hexcase=0; }

What is the author trying to acplish here?

Share Improve this question asked Nov 30, 2011 at 16:07 SteveSteve 31.7k19 gold badges100 silver badges123 bronze badges 1
  • +1 for mentioning my favourite JS crypto site :) – Polynomial Commented Nov 30, 2011 at 16:12
Add a ment  | 

5 Answers 5

Reset to default 8

He is just making sure hexcase is defined, and if it isn't, he is defining it.

Try putting

try {amIdefined} catch(e) {console.log('was not defined');}

in your console and you'll see...

Note that this is the safest way of making sure the variable is defined. In order to do

hexcase = hexcase || 0;

you need to do var hexcase first, or else you will get an error.

If hexcase does not exist, a ReferenceError is thrown, and the catch block is executed. If it does exist, the catch block is not executed.

So it sets hexcase to 0 if it does not exist.

It is a creative way of doing this, though. The more usual way is:

hexcase = window.hexcase || 0; // you have to add window because
                               // otherwise you would still get the error

Looks like hexcase is a global variable that the author is trying to check the existence of. Not sure that's the best way to do it though :-)

I'd go for:

if (typeof hexcase === "undefined") {
    hexcase = 0;
}

Just to make it explicit. You could use this too:

hexcase = hexcase || 0;

he's just checking to see if hexcase was defined, and if not sets a default.

apparantely that will decide whether the result is all uppercase or not...

That try-catch statement will set hexcase to 0 if it's undefined.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>