首页 > 编程语言 > 详细

JavaScript Patterns 4.8 Function Properties - A Memoization Pattern

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!