码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript Patterns 4.8 Function Properties - A Memoization Pattern

时间:2014-06-18 13:17:25      阅读:401      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   color   

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

标签:style   class   blog   code   java   color   

原文地址:http://www.cnblogs.com/haokaibo/p/A-Memoization-Pattern.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!