使用回调的返回值作为另一个函数的参数

时间:2018-10-09 02:41:39

标签: javascript

我已经引用了多个SO问题,但仍然找不到解决方案。这些是我主要研究的问题:

  1. Pass a JavaScript function as parameter
  2. How to execute a method passed as parameter to function

compute.js

const mainTable = document.getElementById('nonFixedSample');

function getRows(metricName) {
    let row = 0;
    let z = document.getElementsByTagName('tr');
    for (let i = 0; i < z.length; i++) {
        if (mainTable.rows[i].firstChild.textContent === metricName) {
            row = i;
            return row;
        }
    }
}

// Here I am trying to pass that function as callback

function stdCellArea(callback) {
    rowNumber = callback();

    let runs = mainTable.rows[rowNumber].cells.length;
    // Other code
}

现在我叫它, reg_report.php

<script>
    stdCellArea(function() {
        getRows('test');
    });
</script>

但是出现以下错误:

  

未捕获的TypeError:无法读取未定义的属性'cells'       在stdCellArea(compute.js:17)       在reg_report.php:39

基本上,我需要将返回值getRows()用作stdCellArea()的参数。我知道我可以简单地做到这一点:

let x = getRows('text');
stdCellArea(x);

但是我必须多次调用此函数,所以我不想创建很多变量。谁可以提供帮助?

1 个答案:

答案 0 :(得分:3)

您需要从回调中返回值:#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #define MAX 51 struct words { char *ch; int index; struct words *pNext; }; struct words* createWordCounter(char *ch) { struct words *pCounter = NULL; pCounter = (struct words*)malloc(sizeof(char)); pCounter->ch = (char*)malloc(strlen(ch)+1); strcpy(pCounter->ch, ch); pCounter->index = 1; pCounter->pNext = NULL; return pCounter; } struct words *pStart = NULL; char* removePunc(struct words* ch) { char *src = ch, *dst = ch; while (*src) { if (ispunct((unsigned char)*src)) { src++; } else if (isupper((unsigned char)*src)) { *dst++ = tolower((unsigned char)*src); src++; } else if (src == dst) { src++; dst++; } else { *dst++ = *src++; } } *dst = 0; } void addWord(char *word) { struct words *pCounter = NULL; struct words *pLast = NULL; if(pStart == NULL) { pStart = createWordCounter(word); return; } pCounter = pStart; while(pCounter != NULL) { if(strcmp(word, pCounter->ch) == 0) { ++pCounter->index; return; } pLast = pCounter; pCounter = pCounter->pNext; } pLast->pNext = createWordCounter(word); } void printWord(struct words *pCounter) { printf("\n%-30s %5d\n", pCounter->ch, pCounter->index); } //sort int compare (const void * a, const void * b){ struct words *A1 = (struct words *)a; struct words *B1 = (struct words *)b; return B1->index - A1->index; /* if ((A1->count - B1->count) > 0) return -1; else if ((A1->count - B2->count) < 0) return 1; else return 0; */ } int main(int argc, char * argv[]) { struct words *pCounter = NULL; char temp[MAX]; FILE *fpt; if(argc == 2) { printf("File name is: %s\n",argv[1]); fpt = fopen(argv[1], "r"); //fail test if(fpt == NULL) { printf("cannot open file, exiting program...\n"); exit(0); } //get the data out of the file and insert in struct int wordCounter = 0; int i = 0; int lines = 0; while((fscanf(fpt, "%s ", &temp)) == 1) { removePunc(temp); addWord(temp); if(temp == ' ') i++; if(temp == '\n') lines++; wordCounter++; } /* pCounter = pStart; while(pCounter != NULL) { printWord(pCounter); pCounter = pCounter->pNext; } */ //sort qsort(pCounter, wordCounter, sizeof(struct words), compare); for(int j = 0; i < 10; i++) { printWord(pCounter); } } fclose(fpt); return 0; } 。否则,return getRows('test');会变成rowNumber,因为这就是没有显式undefined时返回的功能。