javascript中的函数未定义

时间:2022-02-10 08:35:19

i have this js and qunit test below. why the browser gave me the listClasses is not defined? How to solve it. I saw mostly did

我在下面有这个js和qunit测试。为什么浏览器给我的listClasses没有定义?如何解决它。我看到的大多数都是

function ajax() {  
$.ajax({   
});}

but if i did like below how to do the test?

但如果我确实喜欢下面怎么做测试?

$('#MregisteredClasses').on('pageinit', function listClasses(){
var rowInput = "1";
var pageInput = "1";

$.ajax({
    url: 'http://137.57.102.146:8080/Training/getRegisteredClassesData.html',
    data: ( {rows : rowInput , page : pageInput}),
    type: 'POST',

    success: function(json_results){
    $('#list').append('<ul data-role="listview" data-inset="true"</ul>');
        listItems = $('#list').find('ul');
        $.each(json_results.rows, function(key) {
            html = "<li data-mini='true' id='icon'><a href='http://137.57.102.146:8080/Training/MRegisteredClassesDetail.phone?courseId=" 
                   + [json_results.rows[key].courseId] + "&regNo=" + [json_results.rows[key].regNo] +
                   "' rel='external'>" + json_results.rows[key].courseName+ "</a>"
                   + "<a href='http://137.57.102.146:8080/Training/MRateCourse.phone?courseId=" 
                   + [json_results.rows[key].courseId] + "&regNo=" + [json_results.rows[key].regNo] + 
                   "' rel='external'>RATE THIS COURSE</a></li>" ; 
            listItems.append(html); 
        });

        $('#list ul').listview(); 
    },
});
});

and this is qunit test

这是qunit测试

 test('asynchronous test', function() {  
// Pause the test, and fail it if start() isn't called after one second  
stop();  

listClasses(function() {  
    // ...asynchronous assertions  
    ok(true, "Success"); 
});  

setTimeout(function() {  
    start();  
}, 2000);  
});  

2 个解决方案

#1


0  

$('#MregisteredClasses').on('pageinit', function listClasses(){ ... is incorrect usage.

$('#MregisteredClasses')。on('pageinit',function listClasses(){...使用不正确。

The function either needs to be anonymous (i.e. drop the listClasses bit) in which case running listClasses() will fail.

该函数要么是匿名的(即删除listClasses位),在这种情况下运行listClasses()将失败。

What you need to do is externalise the function declaration.

您需要做的是将函数声明外部化。

i.e.:

function listClasses(){
  var rowInput = "1";
  var pageInput = "1";

  $.ajax({
    url: 'http://137.57.102.146:8080/Training/getRegisteredClassesData.html',
    data: ( {rows : rowInput , page : pageInput}),
    type: 'POST',

    success: function(json_results){
    $('#list').append('<ul data-role="listview" data-inset="true"</ul>');
        listItems = $('#list').find('ul');
        $.each(json_results.rows, function(key) {
            html = "<li data-mini='true' id='icon'><a href='http://137.57.102.146:8080/Training/MRegisteredClassesDetail.phone?courseId=" 
                   + [json_results.rows[key].courseId] + "&regNo=" + [json_results.rows[key].regNo] +
                   "' rel='external'>" + json_results.rows[key].courseName+ "</a>"
                   + "<a href='http://137.57.102.146:8080/Training/MRateCourse.phone?courseId=" 
                   + [json_results.rows[key].courseId] + "&regNo=" + [json_results.rows[key].regNo] + 
                   "' rel='external'>RATE THIS COURSE</a></li>" ; 
            listItems.append(html); 
        });

        $('#list ul').listview(); 
    }
});
}

Then call the function from the on command:

然后从on命令调用该函数:

$('#MregisteredClasses').on('pageinit', listClasses)

Assuming your script only failed on listClasses(), then I would assume that the listClasses name is just dropped silently.

假设您的脚本仅在listClasses()上失败,那么我会假设listClasses名称只是静默删除。

#2


1  

There's a difference between function declaration and named function expression.

函数声明和命名函数表达式之间存在差异。

The declaration is of the form:

声明的形式如下:

function myFunc() {
    /* body */
}

Expressions are everything else:

表达式就是其他一切:

var x = function myFunc() {
    /* body */
};

// or

alert(function myFunc() {
    / * body */
});

When a function expression has a name after the function keyword, the function is accessible by this name only inside the function body. Hence your problem.

当函数表达式在函数关键字后面有一个名称时,该函数只能在函数体内访问。因此你的问题。

var x = function myFunc() {
    /* body */
};

x(); // okay
myFunc(); // reference error

More info in Named function expressions demystified.

更多信息在命名函数表达式揭秘。

#1


0  

$('#MregisteredClasses').on('pageinit', function listClasses(){ ... is incorrect usage.

$('#MregisteredClasses')。on('pageinit',function listClasses(){...使用不正确。

The function either needs to be anonymous (i.e. drop the listClasses bit) in which case running listClasses() will fail.

该函数要么是匿名的(即删除listClasses位),在这种情况下运行listClasses()将失败。

What you need to do is externalise the function declaration.

您需要做的是将函数声明外部化。

i.e.:

function listClasses(){
  var rowInput = "1";
  var pageInput = "1";

  $.ajax({
    url: 'http://137.57.102.146:8080/Training/getRegisteredClassesData.html',
    data: ( {rows : rowInput , page : pageInput}),
    type: 'POST',

    success: function(json_results){
    $('#list').append('<ul data-role="listview" data-inset="true"</ul>');
        listItems = $('#list').find('ul');
        $.each(json_results.rows, function(key) {
            html = "<li data-mini='true' id='icon'><a href='http://137.57.102.146:8080/Training/MRegisteredClassesDetail.phone?courseId=" 
                   + [json_results.rows[key].courseId] + "&regNo=" + [json_results.rows[key].regNo] +
                   "' rel='external'>" + json_results.rows[key].courseName+ "</a>"
                   + "<a href='http://137.57.102.146:8080/Training/MRateCourse.phone?courseId=" 
                   + [json_results.rows[key].courseId] + "&regNo=" + [json_results.rows[key].regNo] + 
                   "' rel='external'>RATE THIS COURSE</a></li>" ; 
            listItems.append(html); 
        });

        $('#list ul').listview(); 
    }
});
}

Then call the function from the on command:

然后从on命令调用该函数:

$('#MregisteredClasses').on('pageinit', listClasses)

Assuming your script only failed on listClasses(), then I would assume that the listClasses name is just dropped silently.

假设您的脚本仅在listClasses()上失败,那么我会假设listClasses名称只是静默删除。

#2


1  

There's a difference between function declaration and named function expression.

函数声明和命名函数表达式之间存在差异。

The declaration is of the form:

声明的形式如下:

function myFunc() {
    /* body */
}

Expressions are everything else:

表达式就是其他一切:

var x = function myFunc() {
    /* body */
};

// or

alert(function myFunc() {
    / * body */
});

When a function expression has a name after the function keyword, the function is accessible by this name only inside the function body. Hence your problem.

当函数表达式在函数关键字后面有一个名称时,该函数只能在函数体内访问。因此你的问题。

var x = function myFunc() {
    /* body */
};

x(); // okay
myFunc(); // reference error

More info in Named function expressions demystified.

更多信息在命名函数表达式揭秘。