简体   繁体   中英

Sharing global variables between javascript files loaded to Meteor

I have a javascript file that I place in the client/lib folder within my Meteor app. As the file grew bigger, I decided to split it into 3 files and define an object 'App' in the global namespace in order for the 3 files to share the data.

Each file starts with

var app = app || {};

(function () {
    'use strict';

    app.object1 = {

This way, file2 and file3 can still use app.object1, and so on.

The problem is when Meteor loads the files, it seems to automatically wraps it with function(){}, and this makes app.object1 not accessible from files loaded subsequently.

(function(){
    var app = app || {};

   (function () {
   'use strict';

    app.object1 = {

What is the best way to avoid this issue? Thanks.

EDIT: I referred to this posting [Link:][1] Global variables in Meteor which suggests defining the variable without "var". I replaced the code in file1 to app = {}, but my app is now crashing in file2 in the following line of code, with the message from the Meteor console pasted below.

app.ALL_LIST = 'all'

=> Your application is crashing. Waiting for file change. ReferenceError: app is not defined

在您的变量声明中省略var ;) 然后它将在全局范围内。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM