簡體   English   中英

父參數可以帶子參數嗎?

[英]Can parent parameters take children parameters?

function cached(fn){

  // Create an object to store the results returned after each function execution.

  const cache = Object.create(null);



  // Returns the wrapped function

  return function cachedFn (str) {



    // If the cache is not hit, the function will be executed

    if ( !cache[str] ) {

        let result = fn(str);



        // Store the result of the function execution in the cache

        cache[str] = result;

    }



    return cache[str]

  }

}

根據我的理解 !cache[str] 檢查緩存變量是否是一個包含 str 參數的數組。 這是有道理的,因為數組是 js 中的對象。 它本質上是一種定義數組的奇怪方式。 fn(str) 拋棄了我對代碼的其他理解。 它是繼承的任何例子還是其他什么?

這是一個叫做記憶化的東西的(非常簡化的)例子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM