How to use function argument as part of variable?

前端 未结 2 504
生来不讨喜
生来不讨喜 2021-01-25 00:22

This works

chrome.storage.local.get(\'sizePref\', function(items) { // Get size preferences from storage
  var sizePref2 = items.sizePref.tops; // Set size to a          


        
2条回答
  •  一生所求
    2021-01-25 00:37

    If you are not familiar with promises (viz. unlikely) then you can use callbacks too. I personally think promises are a better way to solve your issue.

    function getSize(itemSize, callback) {
      chrome.storage.local.get('sizePref', function(items) { 
        // Get size preferences from storage
        var sizePref = items.sizePref[itemSize]; //thanks @jfriend00
        callback (sizePref); //thanks at @Kevin Friedheim
      });
    }
    var mySize = getSize(tops, function (mySize) {
        console.log("This size that u are looking for is " + mySize)
    });
    

提交回复
热议问题