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,布布扣,bubuko.com

JavaScript Patterns 4.8 Function Properties - A Memoization Pattern

上一篇:Java学习笔记一:对象与存储


下一篇:C++11中常用的几个简写