2016-09-15 72 views
4

我知道这个问题问了很多次,但我试图找到解决方案,但没有从可用的SO问题中获得。“未捕获的SyntaxError:意外的标识符”

我对Javascript很新鲜。我正尝试在android中使用cordova创建示例计算应用程序。为此,我创建了cordova插件。但是我不断得到两个问题。

"Uncaught SyntaxError: Unexpected identifier", source: file:///android_asset/www/js/index.js (36) 

这里是index.java代码和错误targetting performCalculation()的第一行。

var app = { 

// Application Constructor 
initialize: function() { 
    this.bindEvents(); 
}, 
bindEvents: function() { 
    document.addEventListener('deviceready', this.onDeviceReady, false); 
    document.getElementById("btnCalculate").addEventListener("click", performCalculation); 
}, 
onDeviceReady: function() { 
    app.receivedEvent('deviceready'); 
}, 
// Update DOM on a Received Event 
receivedEvent: function(id) { 
    var parentElement = document.getElementById(id); 
    var listeningElement = parentElement.querySelector('.listening'); 
    var receivedElement = parentElement.querySelector('.received'); 

    listeningElement.setAttribute('style', 'display:none;'); 
    receivedElement.setAttribute('style', 'display:block;'); 

    console.log('Received Event: ' + id); 
} 

performCalculation: function(){ 
    console.log("in index.html"); 
    var success = function() { 
     alert("Success"); 
    }; 
    var error = function(message) { 
    alert("Oopsie! " + message); 
    }; 
    performAddition(20,10,success,error); 
} 

}; 
app.initialize(); 

这是我得到的第二个异常。

"Uncaught SyntaxError: Unexpected token .", source: file:///android_asset/www/js/calculation.js (3) 

这里是calculation.js的代码

var calculationPlugin = { 
console.log("calculation"); 
    performAddition: function(first_number, second_number, successCallback, errorCallback) { 
    console.log("addition"); 
     cordova.exec(
      successCallback, // success callback function 
      errorCallback, // error callback function 
      'CalculationPlugin', // mapped to our native Java class called "CalculationPlugin" 
      'addition', // with this action name 
      [{     // and this array of custom arguments to create our entry 
       "firstNumber": first_number, 
       "secondNumber": second_number, 

      }] 
     ); 
    } 
} 
+1

你有一个缺少的逗号。 – SLaks

+1

这是什么:'console.log(“calculation”);' – Isaac

+0

我补充说,为了日志的目的。 –

回答

5

第一个语法错误

您在receivedEvent函数后面缺少“,”。

二语法错误

计算插件是一个对象,你在里面有控制台,则会引发错误。从该对象中移除控制台。

1

你应该改变这样的:app.receivedEvent( 'deviceready');this.receivedEvent('deviceready');

而且你只有语法错误,如果你把代码行号放在那里会很有帮助。

相关问题