导致"未捕获的SyntaxError:意外令牌的原因。"在我的应用程序?

时间:2014-09-12 14:15:30

标签: javascript jquery backbone.js runtime-error

我的应用程序在以下文件中抛出Uncaught SyntaxError: Unexpected token .

Uncaught SyntaxError: Unexpected token . Tweet.js:2
Uncaught SyntaxError: Unexpected token . Timeline.js:2
Uncaught SyntaxError: Unexpected token . TimelineView.js:2
Uncaught SyntaxError: Unexpected token . app.js:2

但是,当我参考文件时,要检查有问题的行,我看到没有错误:

Tweet.js:

var com = com || {}
, com.apress = com.apress || {}
, com.apress.model = com.apress.model || {}
, com.apress.model.Tweet = Backbone.Model.extend({});

Timeline.js:

var com = com || {}
, com.apress = com.apress || {}
, com.apress.collection = com.apress.collection || {}

[...]

TimelineView.js:

var com = com || {}
, com.apress = com.apress || {}
, com.apress.view = com.apress.view || {}

[...]

app.js:

var com = com || {}
, com.apress = com.apress || {}
, com.apress.view = com.apress.view || {}
, com.apress.view.TimelineView = Backbone.View.extend({
});

$(function() {
    var timelineView = new com.apress.view.TimelineView();
});

我一直得到这些错误并解决它们而不了解我是如何解决它们的。 导致 Uncaught SyntaxError: Unexpected token .错误的原因是什么?如何解决上述错误?

2 个答案:

答案 0 :(得分:2)

.不是变量名中的有效字符,因此您无法执行

var com.apress = ... ;

您应该只有一个声明和简单的赋值,而不是使用带有多个声明的变量声明语句:

var com = com || {};
com.apress = com.apress || {};
com.apress.view = com.apress.view || {};

答案 1 :(得分:-1)

尝试使用括号

进行封装
var com = (com || {})
, com.apress = (com.apress || {}) , ....