I'm running into a funny issue in my (Javascript) code that I think has to do with scope. If this is the case, I'd like to understand scope in JS better.
I doubt this will be relevant, but I'm also working with React and D3.
The gist of my code is as follows:
// 1
export default class C extends Component {
// 2
componentDidUpdate() {
// 3
...
var node = svg.append('svg')
.append('svg:image')
.attr('xlink:href', function(d) {
// 4
return dict['foo'];
});
}
}
I'm trying to use the contents of an outside object within an anonymous function.
dict = {
foo: 'bar.svg'
};
So far I've found that if I declare the above object in locations 3 or 4, the code executes just fine. However, if I move the object declaration outside the method (2), or outside the class entirely (1), I get an error: dict is not defined
.
What about locations 1 and 2 makes them unavailable for use within the anonymous function?