javascript get inner object from function

63 views Asked by At

I have a function in javascript

 var fn = function(){

   var obj = {result: true};

   return obj.result;    
};

I have access to the function, but I need to get the inner obj. Is there a way to do that?


EDIT: Thanks to the answers below, I have managed to come up with a solution. https://stackoverflow.com/users/1447675/nina-scholz 's answer was the closest, so I marked it as the solution.

 var f2 = function fn(){

   fn.obj = {result: true};

   return fn.obj.result;    
};

A client of this function can use it like the following :-

var myFunction = f2;

The function must be called though, to access the inner variables

myFunction();

After that one can do the following.

console.log(myfunction.obj);

This seems to be working. I would, however like to know if this is a good practice.

2

There are 2 answers

0
Nina Scholz On BEST ANSWER

you can access obj when you make it public.

function fn() {
    fn.obj = { result: true };
    return fn.obj.result;
}
console.log(fn());
console.log(fn.obj.result);
0
Konstantin Dinev On

You cannot access the inner object outside of the scope of the function.