Guillermo Rauch, creator of Socket.IO, wrote early Node.js and JavaScript posts about objects, tooling, browser bugs, and conference talks.
HN links 'An Object is not a Hash', which one commenter calls naive and dangerous over reserved property names. Other links cover his tweet-filtering proxy and an RMS anecdote.
It's a bit sad that one of the most useful cases for `Object.create` was not covered: it is the only way to create an object without a prototype. Using constructor functions, even if you explicitly set the ``prototype`` to ``null``, instances's ``__proto__`` remains ``Object.prototype``:
> var F = function () {};
> F.prototype = null;
null
> (new F).__proto__ === Object.prototype;
true
This means all the "default" object methods are there (e.g. #hasOwnProperty) and ready to get clobbered with all the risk it entails[0].
With ``Object.create(null)``, however, the __proto__ of the created object is effectively ``null``:
> Object.create(null).__proto__
null
and such pesky methods as #hasOwnProperty or #toString are not there for clobbering.
And an issue of Object.create failed to be mentioned as well: you can't use ``instanceof`` directly, you have to create one-off "constructor functions" to call ``instanceof`` on them.
[0] http://www.devthought.com/2012/01/18/an-object-is-not-a-hash/
... Yikes. The advice in this post is unfortunately naive. It's dangerous to allow arbitrary object attribute key content. Some object properties in JavaScript must be treated as reserved. Allowing arbitrary access to key names has unexpected repercussions. For example, consider "prototype" or "hasOwnProperty" as key names.
See: http://www.devthought.com/2012/01/18/an-object-is-not-a-hash/ for more details.
So I'd +1 jestar_jokin's advice: use code that's been well-tested. The code presented in the original poster's "Hash Tables in Javascript" article does not look robust.
>The nicest part of it is the built-in hash table type and the related syntactic sugar that everyone for whatever reasons insists on calling "object".
People call it an object because that is what it is. If your code is using Javascript objects as general purpose hash tables with the normal syntactic sugar then your code is broken[1]. The correct way to use an object as a hash table involves a bunch of ugly extra syntax that makes it more verbose than hash tables in any other language I'm aware of.
[1] http://www.devthought.com/2012/01/18/an-object-is-not-a-hash/
No you don't:
http://www.devthought.com/2012/01/24/filtering-tweets-for-your-favorite-client-with-node-js/
Proxlet did this as a service back in the day.
I've been toying with the idea of creating a project based on Guillermo R's post with a social component for sharing regexes and rules. The truth is that this type of local proxy should be a generalizable service for every service you consume.
Also see http://www.devthought.com/2012/01/18/an-object-is-not-a-hash/ from earlier about some pitfalls that you might not immediately consider.
(HN Thread: https://news.ycombinator.com/item?id=4629544)
Similar in C++11: https://github.com/d5/node.native
And a simple example of using it (for a hello world benchmark): http://www.devthought.com/2012/02/08/node-http-hello-world-showdown/