0

I have this situation:

A JavaScript function A(){} and anotherfunction B(){}.

I call function A in two modes:

  • as a parameter of function B -- B(A());
  • in standalone form -- A();

Is there a way to sense in code when function A is evaluated as parameter and when is executed as standalone?

1
  • 4
    No, there is no way to do that. Why would you want to do that? What problem would that solve for you? If you describe that problem, you're much more likely to get a useful answer. Commented May 19, 2013 at 17:40

2 Answers 2

3

Is there a way to sense in code when function A is evaluated as parameter and when is executed as a standalone?

No, because that's not what's happening. The following two code blocks are more or less identical, barring the tiny bit of extra memory for the var

function Implied() {
    B(A());
}

and

function Explicit() {
    var retA = A();
    B(retA);
}

In both cases, A is being called from the parent function, and not by B.

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

Comments

0

I'm not sure why you would want to do that, but you do have a couple options.

One is to simply pass other parameters. This is what you probably should be doing.

Another option is to use call() or apply() to set the value of this.

function B() {
    var context = 'some value';
    A.call(context);  // `this` inside of A will be set to the value of context
}

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.