[Javascript] Keyword 'in' to check prop exists on Object

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Javascript] Keyword 'in' to check prop exists on Object相关的知识,希望对你有一定的参考价值。

function addTo80(n ) {
   return 80 + n;
}

function memoizedAddTo80 (fn) {
  let cache = {};
  return (n) => {
    /*keyword ‘in‘ to check prop exists*/
    if (n in cache) {
        console.log(from cache)
        return cache[n]
    } else {
      console.log(from calculation)
      cache[n] = fn(n)
      return cache[n]
    }
  }
}

const memoAdd = memoizedAddTo80(addTo80);
memoAdd(5) // ‘from calculation‘ 85
memoAdd(5) // ‘from cache‘ 85
memoAdd(6) // ‘from calculation‘ 86

 

以上是关于[Javascript] Keyword 'in' to check prop exists on Object的主要内容,如果未能解决你的问题,请参考以下文章

php中urldecode()和urlencode()起啥作用啊

解决 TypeError: ('Keyword argument not understood:', 'padding')

2019-07-24 运行报错Parsing error: The keyword 'import' is reserved

login() got an unexpected keyword argument 'extra_context'

TypeError: __init__() got an unexpected keyword argument 'strict'

JavaScript搜索框响应事件