Javascript中的错误/警告:循环中的函数

时间:2015-05-08 23:17:07

标签: javascript function function-expression

我是JavaScript的新手,我正在通过在线教程学习它。当前表达式是关于函数表达式的使用,特别是 - 从函数内部返回函数表达式。

以下是代码:

//array of all available rides, with respective wait times
var parkRides = [["Birch Bumpers", 40], ["Pines Plunge", 55], ["Cedar Coaster", 20], ["Ferris Wheel of Firs", 90]];

//array of fast pass rides, indicating next up ride
var fastPassQueue = ["Cedar Coaster", "Pines Plunge", "Birch Bumpers", "Pines Plunge"];

//function to build fast pass queue using array
function buildTicket(allRides, passRides, pick) {

    //if user has picked the first ride in fast pass queue, alert so...
    if(pick == passRides[0]) {

        //remove first item from queue using shift and store it in a var
        var pass = passRides.shift();

        //return function expression here
        return function () {
            alert("QuicK!! You've got a fast pass to " + pass + "!");
        };  //return ends with a ;

    }

    else {
        for (var i = 0; i < allRides.length; i++) {
            if(pick == allRides[i][0]) {

                //return a function expression here
                return function () {
                    alert("A ticket is printing for " + allRides[i][0] +"!\n"+ " Your wait time is about " + allRides[i][1] + " minutes.");
                };
            }

        }
    }

}

//use function buildticket
var wantsRide = "Pines Plunge";

var ticket = buildTicket(parkRides, fastPassQueue, wantsRide);

//execute function expression by calling ticket()
ticket();

我正在使用MS Visual Studio代码,在代码的最后,我收到一个错误和一个警告,即使代码成功运行,我得到了所需的结果,具体取决于wantsRide变量。但是,我无法理解错误和警告的原因。

在屏幕底部(下面的屏幕截图),我得到1个十字和1个感叹号图标,我认为这意味着1个错误和1个警告。

enter image description here

但是当我点击它时,我会在条形图顶部显示一个突出显示的区域! Function inside loop,并突出显示这些行:

return function () {
                    alert("A ticket is printing for " + allRides[i][0] +"!\n"+ " Your wait time is about " + allRides[i][3] + " minutes.");
                };

enter image description here

这里似乎有什么问题?我知道它是for循环中的函数表达式,但为什么会生成警告?是因为if(pick==allRides[i][0])语句可能永远不会成立,所以函数表达式永远不会被执行吗?

1 个答案:

答案 0 :(得分:0)

在黑暗中这是一个镜头,但可能是MSVS无法理解你的函数返回的是什么(即另一个函数)。

这个问题可能会对这个问题有所启发:

Create javascript function that works with intellisense

(或者我可能完全是在错误的树上狂奔)

因此,如果将返回值定义为“function”类型的变量并在以后适当时重新定义它,它在MSVS中可能会更好。 类似的东西:

//function to build fast pass queue using array
function buildTicket(allRides, passRides, pick) {

    var func = new function() {}

    //if user has picked the first ride in fast pass queue, alert so...
    if(pick == passRides[0]) {

        //remove first item from queue using shift and store it in a var
        var pass = passRides.shift();

        func = function () {
            alert("QuicK!! You've got a fast pass to " + pass + "!");
        };  //return ends with a ;
        //return function expression here
        return func;

    }

    else {
        for (var i = 0; i < allRides.length; i++) {
            if(pick == allRides[i][0]) {

                func = function () {
                    alert("A ticket is printing for " + allRides[i][0] +"!\n"+ " Your wait time is about " + allRides[i][1] + " minutes.");
                };
                //return a function expression here
                return func;
            }

        }
    }

}

虽然这并不能解释为什么MSVS会抱怨第二次回复而不是第一次回复(或两者),所以我可能完全错了(我没有或使用过MSVS)。