0

This is probably not possible but i need to ask anyway.

if i have a constructor function something like:

function CS_Sidekick() {
   //do whatever
}
var foo = new CS_Sidekick();

From inside the CS_Sidekick constructor is there any way to figure out the variable that called the constructor, in this case i'd like to get foo.

So something like

function CS_Sidekick(){
   //return foo since that's the variable that called it
}
3
  • You mention returning foo. I've used functions that take a parameter, add some property to it, then return it. In those cases foo exists before you call the function, then you use foo = addNewProps(foo); Commented Sep 20, 2016 at 21:34
  • A "variable" doesn't "call" anything. It's a call, followed by an assignment to a variable. What would you like to get if something else follows? (Hint: no, it's not possible) Commented Sep 20, 2016 at 21:34
  • 1
    This is an XY problem where you have a solution in mind that is not possible and you're asking about that, but you aren't telling us what the real problem is you're trying to solve and thus we can't offer useful solutions to the real problem. Commented Sep 20, 2016 at 21:47

1 Answer 1

2

From inside the CS_Sidekick constructor is there any way to figure out the variable that called the constructor, in this case i'd like to get foo.

No, there is not. The CS_Sidekick() constructor has no idea what the calling code is going to do with the resulting new instance.

You really need to describe what problem you're actually trying to solve because your comments here make no sense:

function CS_Sidekick(){
   //return foo since that's the variable that called it
}

When there is code like this:

var foo = new CS_Sidekick();

The value of foo is not yet set when CS_Sidekick() runs so it really makes no sense for CS_Sidekick() to try to return foo. foo doesn't yet have a value. Besides, the whole point of the callers code:

var foo = new CS_Sidekick();

is to assign the variable foo a new instance of CS_Sidekick(). If that's not what the caller wants, then the caller needs to change their code to something else.

new CS_Sidekick(); should return a new instance of the CS_Sidekick object. That's what it should do.

P.S.

If you want the CS_Sidekick() constructor to know something about it's caller, then you can pass that into the constructor as an argument. There is no automatic way to retrieve information about the calling environment from within the constructor.

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

5 Comments

I guess he meant the magic thing he's looking for was supposed to return the string "foo"
@Bergi - Yeah, maybe. This is an XY problem question where the OP isn't telling us what real problem then are trying to solve.
+1 for "pass an argument".
Hey guys, thanks for the comments. Here's a bit of background that might help clarify the issue. I have developed several templates for others to use in my group at work. Those templates would create functionality, but i'm trying to figure out how they could talk to each other. So for instance, say i create a new CS_Sidekick and assign it to var foo. Then from inside a completely different template i want to find all the CS_Sidekicks that have been defined. If i know that a developer named the var foo then it's fine, but they could name it anything they want. Does that help?
@MikeEllis - If you want to describe the actual template problem you're trying to solve, then I'd suggest you do that in a new question.

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.