6

I have following code:

function f() {

    //...do stuff with arguments 
    //and return something...

}

f(root,
    f(child1),
    f(child2,
        f(subchild1),
        .... 
    ),
);

I want to know when the root level of 'f' is called, so I introduce a flag as an argument:

f(root, '-r',
  f(child1),
  f(child2),
  //...
)

My question is: Is there a way to know when 'f' is called on the top level "f(root,...)" without adding additional arguments?

1
  • You could try the other way around, F()()()(). Here is an example
    – Adelin
    Jul 18, 2018 at 10:56

2 Answers 2

5

No, you have no way of telling, in code within f, that its return value isn't being used to build an arguments list for a subsequent call to f. (Or indeed, that its return value is or isn't being used for any particular thing, or nothing, at all.) It's not an unreasonable question, but that information just isn't available to f.

0

You could actually use f.caller and check if it's null; but it's forbidden in "strict mode" due to tail call optimization: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/caller

Also, it would be unreliable for your type of code: ideally, you shouldn't need such type of branching between the root and the children (see for example any tree structure implementation like the DOM or hyper-script`).

Generally, if you have a f function than returns a tree node, it shouldn't matter at what level is called. For example:

let subchild = f(subchild1)

f(root,
    f(child1),
    f(child2, subchild),
        .... 
    ),
);

That should returns the same structure of your code, but if you will check the caller it won't, since f(subchild1) will be the same as f(root).

1
  • 1
    "...but it's forbidden in "strict mode" due to tail call optimization..." More generally, it's forbidden in strict mode because it's a really bad idea to be assigning the calling function to a property on the called function, not least because while most (overwhelmingly) JavaScript environments have only a single thread accessing a given realm, not all do, nor is shutting out new multi-threaded environments in the future a spec goal. :-) Jul 18, 2018 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.