2016-02-11 2 views
0

У меня есть следующий код:самоосуществляющийся функцию JS

 var currentKey = 0; 
     var totalBinaryMultiplesCollection = {}; 
     for (var row in playField) { 
      if (playField.hasOwnProperty(row)) { 
       alert(row + " -> " + playField[row]); 
       var rowLength = playField[row].length; 
       //Call rowCalc function which returns an array with the binary nrs used in calc 
       var binaryMultiplesRow = rowCalc(rowLength); 
       for(j=0; j < binaryMultiplesRow.length; j++){ 
        //Two methods 
        totalBinaryMultiplesCollection[currentKey] = binaryMultiplesRow[j]; 
        currentKey+=1; 
       } 
      } 
     } 

Я хочу изменить этот код, чтобы быть самостоятельным вызовом функции. Поэтому я добавил следующие вещи:

(function(){ перед блоком кода.
})(); позади блока кода.

Это, однако, дает мне ошибку:

Uncaught TypeError: (промежуточное значение) (промежуточное значение) (промежуточное значение) (...) не является функцией (...).

Я не могу найти здесь проблему. Может ли кто-нибудь сказать мне, что происходит?

Текущая версия:

(function() { 
     var currentKey = 0; 
     var totalBinaryMultiplesCollection = {}; 
     for (var row in playField) { 
      if (playField.hasOwnProperty(row)) { 
       alert(row + " -> " + playField[row]); 
       var rowLength = playField[row].length; 
       //Call rowCalc function which returns an array with the binary nrs used in calc 
       var binaryMultiplesRow = rowCalc(rowLength); 
       for(j=0; j < binaryMultiplesRow.length; j++){ 
        //Two methods 
        totalBinaryMultiplesCollection[currentKey] = binaryMultiplesRow[j]; 
        currentKey+=1; 
       } 
      } 
     } 
    })(); 

и функция rowCalc называют:

var rowCalc = function(rowlength){ 
     var currentRowCollection = []; 
     switch(rowlength) { 
     case 1: 
      currentRowCollection.push(1); 
      break; 
     case 2: 
      currentRowCollection.push(2); 
      break; 
     case 3: 
      currentRowCollection.push(1); 
      currentRowCollection.push(2); 
      break; 
     case 4: 
      currentRowCollection.push(4); 
      break; 
     case 5: 
      currentRowCollection.push(1); 
      currentRowCollection.push(4); 
      break; 
     case 6: 
      currentRowCollection.push(2); 
      currentRowCollection.push(4); 
     case 7: 
      currentRowCollection.push(2); 
      currentRowCollection.push(4); 
      currentRowCollection.push(1); 
      break; 
     default: 
      alert("You made a mistake!") 
     } 
     return currentRowCollection; 
    } 
+0

Можете ли вы опубликовать версию, которая дает вам ошибки? –

+0

Эй, Роб, отредактирован! :) – Kai

+0

На предыдущей строке, похоже, отсутствует точка с запятой. – Bergi

ответ

1

Есть два недостающих запятой в вашей rowCalc функции, вторая из которых вызывает ошибку:

var rowCalc = function(rowlength){ 
    var currentRowCollection = []; 
    switch(rowlength) { 
    case 1: 
     currentRowCollection.push(1); 
     break; 
    case 2: 
     currentRowCollection.push(2); 
     break; 
    case 3: 
     currentRowCollection.push(1); 
     currentRowCollection.push(2); 
     break; 
    case 4: 
     currentRowCollection.push(4); 
     break; 
    case 5: 
     currentRowCollection.push(1); 
     currentRowCollection.push(4); 
     break; 
    case 6: 
     currentRowCollection.push(2); 
     currentRowCollection.push(4); 
    case 7: 
     currentRowCollection.push(2); 
     currentRowCollection.push(4); 
     currentRowCollection.push(1); 
     break; 
    default: 
     alert("You made a mistake!"); 
//        ^
    } 
    return currentRowCollection; 
}; /* 
^ */ 
Смежные вопросы