-1
   listofLOBs: function(data) { //passing data 
        var returnLOB = [];
        console.log(data);
        for (var i1r= 0; i1r < data.length; i1r++) {
                var lob = new LOBslist();
                lob.name = data[i1r].name;
                lob.id = data[i1r].id;
                returnLOB.push(lob); // storing in returnLOB
            }
        console.log(returnLOB[1].name); 
        $('#mainWrapper').find('select[name="lob-select"] option:selected').text(returnLOB[1].id); //using the returnLOB's object data to populate dropdown
        return returnLOB; 
    }

In the above function i am storing objects in the returnLOB array and using it inside the function itself to populate a dropdown.

But say this is file1.js, i would like to access returnLOB's values from file2.js How can i do that? Thanks in advance.

Note: i have declared returnLOB[] globally too.(outside the above function)

Spdexter
  • 923
  • 1
  • 7
  • 19

3 Answers3

0

To do what you want, you just need to remove var from your function so that you're not creating a local variable that shadows the global variable you've created in another file.

listofLOBs: function(data) {
    // Here you're setting a global instead of a local
    returnLOB = [];

You probably know this is a bad idea, and it would be better if you restructure your code so that you don't need a global

A simple solution is to have all code that needs the variable in one place with a self-executing function so your global is only exposed to the code that needs it

(function(){
    var returnLob;

    ... contents of file 1, which write to returnLob

    ... contents of file 1, which read returnLob

})()
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • sorry may be i didnt put my question correct, Say i want to use returnLOB in file 2, how cna i do that? How do i use returnLOB's values? Do i need to call listofLOBs() function? Please help. – Spdexter May 14 '14 at 18:02
  • If you can call `var list = obj.listofLOBs()` from the second file, that is your best choice. That means you must have instantiated your object that has a method `listofLobs()` into the `obj` variable – Ruan Mendes May 14 '14 at 22:33
0

As long as you include file1.js before file2.js, it will work. Take jQuery as an example - or any JS library for that matter - if you include its source file before your code, you can use functions within it.

You want to include the files in the above order, then you can just call the function (listofLOBs()) in file 2.

Additionally, your variable declaration within the function is incorrect.

var returnLOB = [];     // You're declaring a global.

should be

returnLOB = [];      // because the variable is already declared - you just want a local copy.

Hope this helps.

ArtOfCode
  • 5,702
  • 5
  • 37
  • 56
0

You should be able to access any globals between files. In your example you defined returnLOB inside of a function so that won't be accessible but the one you claimed to have defined globally will be accessible by both files.

This also has been asked before so check out:

Javascript: Scope of a Variable across different Javascript files

Community
  • 1
  • 1
Sensei
  • 124
  • 5
  • It won't work, the OP is creating a local variable which is shadowing the global – Ruan Mendes May 14 '14 at 17:51
  • Well ya that's a given, if you name a local variable by the same name then the global is no longer accessible. I figured the poster knew that because the easy solution is to name the local variable differently. – Sensei May 14 '14 at 17:52