I haven't really been able to find a clear and concise answer for this problem I'm having - I'm not even sure how to word it, exactly. But I'm building an object in Javascript (a "class", if you will) that I can reuse. I'm using the JSON approach to objects, as this is the only way I'm familiar with currently (I'm aware of the other methods, I just haven't been able to sit down and experiment with them yet).
So, here's an example of what I'm doing:
var Thing = {
'property':{
'one':5,
'two':3
},
'action':(function(arg1, arg2){
alert(arg1 + ' ' + arg2);
return this.property.one + this.property.two;
});
}
var t = Thing;
alert(t.action('x', 'y'));
As you can see, I've got my object, and it's got some nested properties. There's also a function, which is supposed to be able to manipulate said properties.
The problem I'm having is with scope. I'm tring to find a way to reference the object's properties within the function with out it being assumed that the properties BELONG to that function, because they don't. I've done it in the past, where I've just passed "this" as a parameter to the function and everything worked fine, but I'm willing to bet that isn't good practice. Also, as you can see, this function already takes some arguments anyway. Running this code give an error: "Uncaught TypeError: Cannot read property 'one' of undefined".
I'd like to keep my methods for building this object intact - it's something for work, and the object is already pretty large; but what I'm looking for is someway to "reach back up the tree" to get at those properties.