JavaScript Patterns 4.8 Function Properties - A Memoization Pattern
Gets a length property containing the number of arguments the function expects:
function func(a, b, c) {}
console.log(func.length); //
var myFunc = function () {
// serialize the arguments object as a JSON string and use that string as a key in your cache object
var cachekey = JSON.stringify(Array.prototype.slice.call(arguments)),
if (!myFunc.cache[cachekey]) {
var result = {};
// ... expensive operation ...
myFunc.cache[cachekey] = result;
}
return myFunc.cache[cachekey];
};
// cache storage
myFunc.cache = {};
References:
JavaScript Patterns - by Stoyan Stefanov (O`Reilly)
JavaScript Patterns 4.8 Function Properties - A Memoization Pattern的更多相关文章
- JavaScript Patterns 5.3 Private Properties and Methods
All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return ...
- JavaScript Patterns 6.5 Inheritance by Copying Properties
Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...
- JavaScript Patterns 7.1 Singleton
7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. Th ...
- JavaScript Patterns 6.6 Mix-ins
Loop through arguments and copy every property of every object passed to the function. And the resul ...
- JavaScript Patterns 6.4 Prototypal Inheritance
No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...
- JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance
// the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...
- JavaScript Patterns 5.7 Object Constants
Principle Make variables shouldn't be changed stand out using all caps. Add constants as static prop ...
- JavaScript Patterns 5.5 Sandbox Pattern
Drawbacks of the namespacing pattern • Reliance on a single global variable to be the application’s ...
- JavaScript Patterns 5.4 Module Pattern
MYAPP.namespace('MYAPP.utilities.array'); MYAPP.utilities.array = (function () { // dependencies var ...
随机推荐
- 背水一战 Windows 10 (1) - C# 6.0 新特性
[源码下载] 背水一战 Windows 10 (1) - C# 6.0 新特性 作者:webabcd 介绍背水一战 Windows 10 之 C# 6.0 新特性 介绍 C# 6.0 的新特性 示例1 ...
- 一个十年java程序员的心得
展望未来,总结过去10年的程序员生涯,给程序员小弟弟小妹妹们的一些总结性忠告 走过的路,回忆起来是那么曲折,把自己的一些心得体会分享给程序员兄弟姐妹们,虽然时代在变化,但是很可能你也会走我已经做过的1 ...
- 编译gcc
下载源码 自GNU FTP站下载GCC. 自Infrastructure页面下载四个库的源代码,即GMP.MPFR.MPC以及ISL(ISL非必需). 也可以不手工下载,执行源码中的./contrib ...
- 值栈(Structs2)
1. 关于值栈: 1). 登陆 时, ${userName} 读取 userName 值, 实际上该属性并不在 request 等域对象中, 而是从值栈中获取的. 2). ValueStack: I. ...
- (学习笔记)HTML的<link>标签
在HTML中<link>标签用于定义文档与外部资源的关系. <link>标签只存在于head部分. <head> <link rel="styles ...
- web项目 验证码 *** 最爱那水货
1. jsp代码 : <Script> function changeImg(){ document.getElementById("certImg").src =&q ...
- linux下搭建php的集成环境
一个偶然的机会,在项目中需要搭建PHP的环境,由于PHP开发需要的东西比较多,像apache.mysql.PHP环境等,如果一个一个装很可能会有安装不全的问题,为此选择了安装集成环境,这里选择的是xa ...
- Android提升篇系列:adb无法识别MX5等特殊机型
发现自己Ubuntu系统adb无法识别魅族 mx5机型.操作具体如下(其他机型依然适用): 一.Ubuntu环境 1.查看自己当前设备的idVendor lsusb命令直接查看当前usb设别列表,找到 ...
- Android总结篇系列:Android开发环境搭建
工欲善其事必先利其器. 1.安装并配置Java环境进入Java oracle官网,当前网址如下:http://www.oracle.com/technetwork/java/javase/downlo ...
- ActiveMQ 简介与安装
一. 概述与介绍 ActiveMQ 是Apache出品,最流行的.功能强大的即时通讯和集成模式的开源服务器.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provide ...