0

I change places function so that the caller is before called, an error occurs.

What can say this character? Maybe somewhere in the distance of a kilometer bracket is not closed?

UPD: For example, its code work correctly if I place first string at end:

SCRIPT5022: Pass a function that returns the value of the dependentObservable knockout-2.0.0.debug.js, line 1054 character 9

osagoViewModel.fields.yearsBoxes = new field("Years", yearsBoxesFunc, null, osagoViewModel);


function yearsBox() {
    this.year = new field("Years", function () { return ["1 year", "2 years", "3 years", "4 years", "5 years", "6 years", "7 years", "8 years", "9 years", "10 years"]; }, null, osagoViewModel);
}


var yearsBoxesFunc = function () {
    var yearsBoxCount = osagoViewModel.fields.driversCount.selectedValue();

    var retArrFunc = function (count) {
        var arr = [];
        for (var i = 0; i < count; i++) {
            arr.push(new yearsBox());
        }
        return arr;
    };


    switch (yearsBoxCount) {
        case "many":
            return retArrFunc(0);
        case "1":
            return retArrFunc(1);
        case "2":
            return retArrFunc(2);
        case "3":
            return retArrFunc(3);
        case "4":
            return retArrFunc(4);
        case "5":
            return retArrFunc(5);
    }
}
FreeVice
  • 2,567
  • 6
  • 21
  • 28

3 Answers3

1

it depends how you declare the function. there is a difference between if you use var or not. have a look on this sample:

function FunctionDefinitionOrder() {
    assert(isRed(), "Named function definition doesn't matter");        
    assert(isGreen === undefined, "However, it's not the case with anonymous functions");

    function isRed() { return true; }
    var isGreen = function () { return true; };

    assert(isGreen(), "Anonymous functions are known only after the definition");
}
Sagiv Ofek
  • 25,190
  • 8
  • 60
  • 55
1

You should revert your changes and be on the latest stable state. And then start making minor changes whatever you want to do. This will take a lot of time to identify any syntax error in the code.

Tahir
  • 3,344
  • 14
  • 51
  • 69
1

Open your console and let it halt on errors. In the stacktrace you will see where you called the knockout.js-function without a function as an argument.

To read about parsing of functions and their availability in the current scope, read What is the difference between a function expression vs declaration in JavaScript? and/or var functionName = function() {} vs function functionName() {}.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375