js0005 发表于 2018-12-10 | 阅读次数: 字数统计: 133 | 阅读时长 ≈ 1 1234567891011121314151617181920212223242526272829303132<!DOCTYPE html><html><head> <title>Storing a collection of unique functions</title> <meta charset="utf-8"> <link rel="stylesheet" type="text/css" href="../assert.css"> <script src="../assert.js"></script></head><body> <script> var store = { nextId: 1, cache: {}, add: function(fn) { if (!fn.id) { fn.id = this.nextId++; this.cache[fn.id] = fn; return true; } } }; function ninja(){} assert(store.add(ninja), "Function was safely added."); assert(!store.add(ninja), "But it was only added once."); </script></body></html>