[removed] Passing constructor as parameter, function instantiates only a single object

后端 未结 1 431
你的背包
你的背包 2021-01-28 04:32

I\'d like a certain function to create a grid and insert a unique object in each slot. The key here is, the function should accept the constructor as a parameter (as there can b

相关标签:
1条回答
  • 2021-01-28 04:58

    Pass the buildNewObject as a function, instead of calling it and passing its result.

    function newGrid(rows,cols,dataFunction) {
        var customGrid = [];
        for (var row=0;row<rows;row++){
            var customRow = [];
            for (var col=0;col<cols;col++){
                var data = dataFunction(); // yes, it's a function
                                           // we need (want) to call it
                customRow.push(data);
            }
            customGrid.push(customRow);
        }
        return customGrid;
    }
    

    var myGrid = newGrid(3,3,buildNewObj); // no parenthesis, no invocation
    
    0 讨论(0)
提交回复
热议问题