Solved

Accessing variable declared in one javascript file from another..

Posted on 2016-10-20
1
48 Views
Last Modified: 2016-10-25
Hi,
I have the following structure of my javascript files... I am using webpack as the module bundler :

sidebar.js
-------------------
var ring = require('./ring.js');

$(document).ready(function() {
    var prod = $('#data').data();
    ring.setIsProd(prod);
});
-------------
ring.js
-------------
var filter = require('./filter.js');
var prod;
exports.setIsProd = function(prod) {
  this.prod = prod;
}
-------------
filter.js
-------------
Now in this file how do i access the value of prod ??
I tried using this.prod but didnt work.


Also I have some more variables that i want to set in ring...
like ring.setUserID(userID);

so i tried making one more function
exports.setUserId = function(userId) {
    this.userId = userId;
}

in ring.js same as isProd...
Any suggestions for an alternate way to do this ?
As if the number of such properties increase it looks really unclean to have so many exports

The idea behind the code is the ring.js file is a library which any user will be including inside its own js file which in this case is sidebar.js...  and apart from other work one major job is to include a filter in user page... so i separated this functionality into filter.js
But user can only interact with ring.js...and depending on prod value the filter will return a filter containing either prod entries or staging entries... So it needs the prod value..

Thanks
0
Comment
Question by:Rohit Bajaj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 57

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41853512
One option is to have your variables in an object and a generic get function

var data = {
  prod: 'value1',
  something: 'value2',
...
}
exports.getVar = function(id) {
   var ret = data[id] || false;
   return ret;
}

Open in new window


EDIT: Changed bad choice of parameter name 'var' for 'id'
0

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
boostsrap 1 32
Set css in function 11 53
Time Picker 3 21
javascript duplicates 9 24
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question