?
Solved

Accessing variable declared in one javascript file from another..

Posted on 2016-10-20
1
Medium Priority
?
51 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 58

Accepted Solution

by:
Julian Hansen earned 2000 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

7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
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)
Suggested Courses

770 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