!!是一个很简单的方式,让任意JavaScript表达式变成Boolean的方式, 例如:!!“hello” === true and !!0 === false
参考: 忍者禁术
var store = {
nextId: 1, cache: {},
add: function(fn){ if (!fn.id){ fn.id = store.nextId++;
return !!(store.cache[fn.id] = fn); } } }
function ninja(){};
assert(store.add(ninja), "Function was safely added.");
assert(!store.add(ninja), "But it was only added once.");