Solved

Accessing variable declared in one javascript file from another..

Posted on 2016-10-20
1
36 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 51

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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)

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now