Secret of the JavaScript Ninja 学习笔记 - 6
来源:互联网 发布:吉他谱编配软件 编辑:程序博客网 时间:2024/04/30 05:45
第四章 Wielding functions
4.3 Fun with function as objects
var fn = function() {};
The semicolon after function(){} is a good practice. When compressing code, properly placed semicolons will allow for greater flexibility in compression techniques.
var fn = function(){};fn.prop = "tanuki";
We can attach properties to a function.
4.3.1 Storing functions
We want to store a collection of related but unique functions, event callback management being the most obvious example. When adding functions, a challenge is determining which functions are actually new to the collection and should be added, and which are already resident and shouldn't be added.
var store = { nextId: 1, cache: {}, add: function(fn) { if (!fn.id) { fn.id = store.nextId++; return !!(store.cache[fn.id] = fn); } } }; function ninja() {}; store.add(ninja); store.add(ninja);
Note: the !! construct is a simple way of turning any JavaScript expression to its Boolean equivalent.
4.3.2 Self-memoizing functions
Memoization is the process of building a function that's capable of remembering its previously computed values.
function isPrime(value) { if (!isPrime.answers) isPrime.answers = {}; if (isPrime.answers[value] != null) { return isPrime.answers[value]; } car prime = value != 1; for (var i = 2; i < value; i++) { if (value % i == 0) { prime = false; break; } } return isPrime.answers[value] = prime; }
- Secret of the JavaScript Ninja 学习笔记 - 6
- Secret of the JavaScript Ninja 学习笔记 - 1
- Secret of the JavaScript Ninja 学习笔记 - 2
- Secret of the JavaScript Ninja 学习笔记 - 3
- Secret of the JavaScript Ninja 学习笔记 - 4
- Secret of the JavaScript Ninja 学习笔记 - 5
- Secret of the JavaScript Ninja 学习笔记 - 7
- Eloquent JavaScript 笔记 六:The Secret Life of Objects
- 翻译 Secrets of the JavaScript Ninja (JavaScript忍者禁术)
- 翻译 Secrets of the JavaScript Ninja - 1.进入忍者的世界(1.Enter the ninja)
- The secret of ROWID
- secrets of the javascript Ninja( javascript Timer)(javascript忍者的秘密)
- secrets of the javascript Ninja (Function Prototypes)(javascript忍者的秘密)
- secrets of the javascript Ninja( with(){} 的用法)(javascript忍者的秘密)
- secrets of the javascript Ninja(Function Type)(javascript忍者的秘密)
- 翻译 Secrets of the JavaScript Ninja - 2.测试和debug(2.Testing and debugging)
- 翻译 Secrets of the JavaScript Ninja - 3.函数是根基(3.Functions are fundamental)
- 翻译 Secrets of the JavaScript Ninja - 4.挥舞函数(4.Wielding functions)
- 专访蒋彪:JavaEE是企业级开发首选(转载)
- QT210 自制bootloader实现uboot command 和串口烧写bootloader
- hdu - 1042 - N!(高精度计算)
- Epic Games创始人Tim Sweeney:头戴显示技术将颠覆电子产业
- 微软.NET各技术应用前景 针对vs.net2010
- Secret of the JavaScript Ninja 学习笔记 - 6
- hdu - 2111 - Saving HDU
- hdu - 1237 - 简单计算器
- hdu - 1201 - 18岁生日
- net-snmp安装配置
- HDU 1317 POJ 1932 XYZZY 正负环+最长路
- 微软认证考试70-461 Modify Data 数据修改 --24%比重--(1)
- 如何看懂oracle 直方图
- [Leetcode] Jump Game ii