Solved

Accessing variable declared in one javascript file from another..

Posted on 2016-10-20
1
43 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
1 Comment
 
LVL 54

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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 …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

786 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