var obj = {a:1,b:2};
function parent_object(num){
console.log('This number is inside the object ',<????>);
}
parent_object(obj.a)
This should output pointing to obj. The only way I found to do this is boxing every number object in an object which would keep a reference to the parent. This is, though, a performance killer. Is there a better way of keeping track of the location of a number?
Actually no. And not only in JavaScript, but in none of the other conventional languages.
As far as I know, the only way would be for you to do exactly what you yourself said: boxing the number (or any other field type) in an object that would keep a reference (maybe by constructor injection) to its enclosing parent.
The boxing in question can be thus accomplished:
However, generally speaking, the most important factor in your JavaScript application’s performance for current browsers is the way you handle your memory usage. Therefore, this approach isn’t really advisable, and I suggest you solve your problem, whatever it might be, via some other method.