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); // 3

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 = {};

JavaScript Patterns 4.8 Function Properties - A Memoization Pattern

时间: 2024-10-06 14:49:07

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 this.myprop; } }; console.log(myobj.myprop); // `myprop` is publicly accessible console.log(myobj.getProp()); // getProp() is public too The same is

JavaScript Patterns 6.5 Inheritance by Copying Properties

Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) { if (parent.hasOwnProperty(i)) { child[i] = parent[i]; } } return child; } Deep copy pattern function extendDeep(parent, child) { var i, toStr = Obje

JavaScript Patterns 5.4 Module Pattern

MYAPP.namespace('MYAPP.utilities.array'); MYAPP.utilities.array = (function () { // dependencies var uobj = MYAPP.utilities.object, ulang = MYAPP.utilities.lang, // private properties array_string = "[object Array]", ops = Object.prototype.toStr

JavaScript Patterns 7.1 Singleton

7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. This means that the second time you use the same class to create a new object, you should get the same object that was created the first time. var obj =

JavaScript Patterns 2.12 Writing API Docs

Free and open source tools for doc generation: the JSDoc Toolkit (http://code.google.com/p/jsdoc-toolkit/) and YUIDoc (http://yuilibrary.com/projects/yuidoc). Process of generating API documentation ? Writing specially formatted code blocks ? Running

JavaScript Patterns 2.10 Naming Conventions

1. Capitalizing Constructors var adam = new Person(); 2. Separating Words camel case - type the words in lowercase, only capitalizing the first letter in each word. upper camel case, as in  MyConstructor(), lower  camel  case,  as  in  myFunction(),

JavaScript Patterns 3.2 Custom Constructor Functions

When you invoke the constructor function with new, the following happens inside the function: ? An empty object is created and referenced by this variable, inheriting the prototype of the function. ? Properties and methods are added to the object ref

JavaScript Patterns 5.5 Sandbox Pattern

Drawbacks of the namespacing pattern • Reliance on a single global variable to be the application’s global. In the namespacing pattern, there is no way to have two versions of the same application or library run on the same page, because they both ne

JavaScript Patterns 2.5 (Not) Augmenting Build-in Prototypes

Disadvantage Other developers using your code will probably expect the built-in JavaScript methods to work consistently and will not expect your additions. Properties you add to the prototype may show up in loops that don't use hasOwnProperty(), so t