Consider an alternative "solution" that a person might come up with -
creating a "truthy" property for every "seen" value and then performing
a plain boolean type conversion to determine value uniqueness:
....
if (obj[string]) {
....
else {
obj[string] = true;
....
It's clear that this approach falls short with values identical to any
of the `Object.prototype` properties' names, since such values would be
considered "seen" by a boolean type conversion (after being resolved
from `Object.prototype` as Function objects and so type converted to
`true` as any other object): -
var arr = ['toString'];
uniquify(arr);
arr; // []
An obvious "fix" for this might be to not perform type conversion, and
instead compare values explicitly:
....
if (obj[string] === true) {
....
else {
obj[string] = true;
....
While better than a previous "solution", this one still has a chance of
collisions, and a quite large one at that: -
Object.prototype.foo = true;
var arr = ['foo'];
uniquify(arr);
arr; // []
An `Object.prototype.hasOwnProperty` would of course solve this problem,
but is unfortunately not always available in modern (and not so modern)
browsers (Safari 2.x comes to mind):
....
if(Object.prototype.hasOwnProperty.call(obj, string)) {
...
}
else {
obj[string] = true; // or any value for that matter,
// even `undefined` will do

}
Unique "key", therefore, solves this problem quite painlessly.