开发者

JavaScript Integer Leading to NaN

I have JavaScript that basically looks like the following

function a() {
    b(1);
}

function b(myNumber) {
    c(myNumber);
}

function c(myNumber) {
    var calculation = 5 * (myNumber - 1);
    alert(calculation);
}

When I call the function a(), the alert box ends up saying "NaN". Why is this happening? I've tried using the parseInt() function in a number of places, but nothing seems to work.

EDIT

Full code (what's actually being done rather than a stripped down example):

function updateTablePagination(tableId, rowsPerPageSelectId) {
    updateTablePagination(tableId, rowsPerPageSelectId, 1);
}

function updateTablePagination(tableId, rowsPerPageSelectId, pageNumber) {
    var table = document.getElementById(tableId);
    var rowsPerPageSelect = document.getElementById(rowsPerPageSelectId);
    var rowsPerPage = rowsPerPageSelect.options[rowsPerPageSelect.selectedIndex].text;

    updateTable(table, rowsPerPage, pageNumber);
    //updateTablePageLinks();
}

function updateTable(table, rowsPerPage, pageNumber) {
    var tableRows = table.getElementsByTagName("tr");
    var totalNumberOfRows = tableRows.length;

    var startRow = rowsPerPage * (pageNumber - 1);      
     var endRow = Math.min(startRow + rowsPerPage, totalNumberOfRows - 1);

    alert("Start: " + startRow + "\nEnd: " + endRow);
}

A select box has an onchange开发者_如何学JAVA calling updateTablePagination('myTableId', 'rowsPerPage'). The ids are both correct.

"Start" and "End" are both NaN.

Edit 2

Alternatively, if I just do alert(pageNumber), it is undefined.

Simplified

Even this says pageNumber is undefined:

function updateTablePagination(tableId, rowsPerPageSelectId) {
    updateTablePagination(tableId, rowsPerPageSelectId, 1);
}

function updateTablePagination(tableId, rowsPerPageSelectId, pageNumber) {
    alert(pageNumber);
}


You have two functions called updateTablePagination. Javascript does not support function overloading. Get rid of the first declaration, because it is getting overwritten with the second. You can use the || to define a default value for the parameter.

function updateTablePagination(tableId, rowsPerPageSelectId, pageNumber) {
    pageNumber = pageNumber || 1; //Set a default value for pageNumber
    var table = document.getElementById(tableId);
    var rowsPerPageSelect = document.getElementById(rowsPerPageSelectId);
    var rowsPerPage = rowsPerPageSelect.options[rowsPerPageSelect.selectedIndex].text;

    updateTable(table, rowsPerPage, pageNumber);
    //updateTablePageLinks();
}


updateTablePagination(tableId, rowsPerPageSelectId) //Will call the function with pageNumber == 1


The problem is that you're overwriting functions. With this:

function a() {
    a(1)
}

function a(x) {
    alert(x);
}

calling a will always call the second (overwritten) function and thus x is always undefined. You want something like overloading. Best thing to do in that case is using the || operator for a default value:

function a(x) {
    var x = x || 1; // x if x is given, otherwise 1
    alert(x);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜