首页 > 网站 > WEB开发 > 正文

JavaScript Patterns 4.8 Function Properties

2024-04-27 14:22:36
字体:
来源:转载
供稿:网友

javaScript Patterns 4.8 Function PRoperties - A Memoization Pattern

2014-06-16 23:17 by 小郝(Kaibo Hao), ... 阅读, ... 评论, 收藏, 编辑

Gets a length property containing the number of arguments the function expects:

function func(a, b, c) {}console.log(func.length); // 3var 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 storagemyFunc.cache = {};

References:

Javascript Patterns -by Stoyan Stefanov(O`Reilly)


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表