6

I'm writing an application in javascript and cannot figure it out how to access the variables declared in my function, inside this jquery parse. Inside I can access global variables, but I don't really want to create global vars for these values.

Basically I want to extract file names from an xml document in the simulationFiles variable. I check if the node attribute is equal with the simName and extract the two strings inside the xml elements, that part I think it's working.

How can I extract those xml elements and append them to local variables?

function CsvReader(simName) {
    this.initFileName = "somepath";
    this.eventsFileName = "somepath";
    $(simulationFiles).find('simulation').each(function() {
       if ($(this).attr("name") == simName) {
           initFileName += $(this).find("init").text();
           eventsFileName += $(this).find("events").text();
       }
    });
}   

3 Answers 3

14

The this in the CsvReader function is not the same this in the each() callback (where instead it is the current element in the iteration). To access the scope of the outer function within the callback, we need to be able to reference it by another name, which you can define in the outer scope:

function CsvReader(simName) {
    this.initFileName = "somepath";
    this.eventsFileName = "somepath";
    var self = this; // reference to this in current scope
    $(simulationFiles).find('simulation').each(function() {
       if ($(this).attr("name") == simName) {
           // access the variables using self instead of this
           self.initFileName += $(this).find("init").text();
           self.eventsFileName += $(this).find("events").text();
       }
    });
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thx for the quick answer, but unfortunately that doesn't work. Actually I tried it too before, but inside the unnamed function the $self is reported as not defined by firebug.
It works like this! Sorry about that, when I saved the this to a variable I had a different error after this function call and when I debugged the debugger showed me that $self.initFileName is undefined inside, but actually it was working and and I got the result. Thanks very much!
Are your this.initFileName and this.eventsFileName supposed to be local to your function or to the entire page? Nm... saw your post a few seconds later. Glad it works!
@CantucciHQ: Because the this in the CsvReader function is not the same this in the each() callback. To access the scope of the outer function within the callback we need to reference it by another name. Having the $self reference allows that to happen.
@Cory Thanks a lot, that makes sense. I was even more confused yesterday -- I wasn't finding variables inside a $.each() using the Chrome dev tools. But it turns out the variable didn't exist inside the each() because the variable wasn't already being used in the each() ! Once I added a usage of the variable in the actual code, all worked well.
|
4

I made a working demo (I changed it to use classes so it would work with HTML).

function CsvReader(simName) {
    this.initFileName = "somepath";
    this.eventsFileName = "somepath";
    var context = this;
    $(simulationFiles).find('simulation').each(function() {
       if ($(this).attr("name") == simName) {
           context.initFileName += $(this).find("init").text();
           context.eventsFileName += $(this).find("events").text();
       }
    });
}   

1 Comment

This method worked great, thx! Sorry but the debugger miss leaded me a bit and taught it wasn't working.
1

The simplest change you can do to make it work is... Change your function in each from normal ( function() {}) to arrow function ( () => {} ) that will automatically take the context of the function in which it is defined.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.