我在前端有一个在Backbone上运行的应用程序,当浏览器加载页面时,它会在控制台中返回错误:
Uncaught ReferenceError: app is not defined
at index.js:6
at index.js:51
(anonymous) @ index.js:6
(anonymous) @ index.js:51
/* global app:true */
(function() {
'use strict';
app = app || {}; // error here although it must prevent of undefined
// variables stay undefined
//.......
//backbone code here
//.......
$(document).ready(function() {
app.contactView = new app.ContactView(); //error here
});
}());
从构建切换到CDN或进入Backbone.Model属性会返回相同的错误。
答案 0 :(得分:2)
如果您希望变量是全局变量,则仅将变量移出函数范围。
>>> re.sub(r'\.(?=[^ \W\d])', '. ', 'Foo bar.Baz Inc., foobar. 1.1, and abc._')
'Foo bar. Baz Inc., foobar. 1.1, and abc. _'
或将其作为IIFE的参数传递。
>>> re.sub(r'\.(?=[^ \W\d])(?=[^\w*]/)', '. ', 'Foo bar.Baz Inc., foobar. 1.1, and abc._ http://www.example.com/whatever')
'Foo bar.Baz Inc., foobar. 1.1, and abc._ http://www.example.com/whatever'
如果您有多个文件,它们都应该实现与上面示例类似的结构,以便能够使用/* global app:true */
var app = app || {};
(function() {
'use strict';
//.......
//backbone code here
//.......
// you can use the jQuery shortcut version of the document ready.
$(function() {
app.contactView = new app.ContactView();
});
}());
命名空间。