3

In JavaScript, functions can have properties. Like this:

var f = function(){ console.log("Hello!"); }
f.x = "Whoohoo";

How do I retrieve x from code within f() that might be called long after the variable f goes out of scope?

1
  • 2
    If you're within the body of function f, how is it possible for it to be "out of scope"? Just use f.x in the body. Commented Dec 7, 2016 at 18:30

4 Answers 4

4

You could use the property accessor, like the assignment.

var f = function() { console.log("Hello! " + f.x); }
f.x = "Whoohoo";

console.log(f.x);
f();

For stable access, you could use a named function

var f = function foo() { console.log("Hello! " + foo.x); }
//               ^^^ >>>>>>>>>>>>>>>>>>>>>>>>>>> ^^^
f.x = "Whoohoo";

console.log(f.x);
f();

Sign up to request clarification or add additional context in comments.

5 Comments

You misunderstood; I want to get the property from code within the function itself, not from the code that surrounds it.
@SevaAlekseyev, it is stil the property accessor. for a static reference, i suggest to use a named function.
Works. It wasn't immediately obvious to me that the closure logic would capture the function variable, since it hasn't been assigned at the time of function creation.
Nice - using a named function would be preferable to the function referencing f
@SevaAlekseyev Closures do capture bindings (variables), not the values that they contain when the closure is created.
0

If you need some more robust (but also more verbose):

var f = (function(){
    var func = () => {
        console.log(func.a)
    }
    return func
})()
f.a = 'Whoohoo'

// trying to break it
var a = f
f = 'somethingelse'
a()

Comments

0

If what you're looking for is to simply access that property from within your function f, you simply reference the function object (either by the variable name if it's a function expression, or by the function name if it's a function declaration).

For example:

var f = function(){ return f.x; };
f.x = "Whoohoo";

// f() => "Whoohoo"

var b = function(){ return f.x; ];

// b() => "Whoohoo"

However, your mention of calling f "long after the variable f goes out of scope" contradicts the lexical scoping of JavaScript. You cannot call the function f unless you do so from the same scope where the function was defined, or from a child scope in reference to where the function was defined. If you are "out of scope", invoking f will throw a reference error.

1 Comment

setTimeout(f, 1);
0

Let's get a little convoluted. f is already the name of the function by the expression that defines it. And a function is an object by definition in JS. So you can access it and it's properties (such as x) through the f keyword like f.x. Yet, since f is a function, you may use the it as a constructor too and do silly things like...

var f = function(){ console.log("Hello!"); };
f.x = "Whoohoo";

f.prototype.thingy = function(){
  console.log(this.constructor.x);
};

var a = new f();
a.thingy();

... and from this point on lets get a little more convoluted. You might event do;

function F(){
  F.prototype.thingy = function(){
                         return this.constructor.x;
                       };
  var obj = Object.create(F.prototype);
  console.log("Hello!");
  return obj.thingy();
}


F.x = "Whoohoo";

console.log(F());

So you can get the value of a property of the function object F "from within itself" by taking an object instantiated by itself under a closure.

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.