1

I am developing a lib currently, which is relying on arguments.callee.caller. This is not compatible with "use strict", so it throws an error when the caller function was defined in strict mode. I catch those errors, they does not really matter, since the important part is not defined in strict mode. Is there an environment which supports only strict mode, and so is not compatible with this lib?

inf3rno
  • 24,976
  • 11
  • 115
  • 197
  • 2
    Node can be forced to execute all code in strict mode with a command-line flag if that is what you want to know. – Bergi Aug 23 '16 at 17:35
  • What do you need `.caller` for? – Bergi Aug 23 '16 at 17:35
  • @Bergi Thanks! I wanted to know something like that. :-) I use the caller to do something like `Error.captureStackTrace(o, fn)` does in a non-v8 environment: https://github.com/inf3rno/e3/blob/master/lib/stackAdapter/StackAdapter/non-v8/FrameStringSource.js#L16 I test currently for `arguments.callee.caller` to do graceful degradation, but that test does not catch the error by strict mode. I need to modify that. – inf3rno Aug 23 '16 at 19:21
  • @Bergi Is there any non-node environment which enforces strict mode? Or is there at least one browser which does not support arguments.callee.caller, but supports ES5? The current code uses `Error.prepareStackTrace()` by node and chrome, so they are not affected. – inf3rno Aug 23 '16 at 19:38
  • I don't know any (which doesn't mean there are none), but i'm not sure why you'd want to know. Either there is one, and you don't support it, or there is none and everything is ok. Just wrap the access attempt in a `try`/`catch` and be done with it. – Bergi Aug 23 '16 at 19:42
  • Btw, you might want to have a look at all the stack-trace-retrieving libraries out there and see how they do it. – Bergi Aug 23 '16 at 19:43
  • @Bergi I want to decide whether I want to support those environments. That's the point of this SO question... – inf3rno Aug 23 '16 at 19:52
  • @Bergi Afaik only https://github.com/stacktracejs/stacktrace.js is worth to mention, but it does browser detection and has it's own api. I don't like their approach. – inf3rno Aug 23 '16 at 19:54
  • @Bergi Add an answer with the command line code that forces strict mode, so I can accept it. – inf3rno Aug 23 '16 at 19:55
  • I guess if there actually are any such environments, then you *cannot* support them anyway - at least not without finding another trick. But regardless, I recommend YAGNI - if nobody knows such an environment, it's not relevant. But if you really want to know, check the lists of supported environments of other stack-tracing libs. – Bergi Aug 23 '16 at 19:56
  • @Bergi Actually I can more or less support it, but it is much harder than with `arguments.callee.caller`. I'll check, but I don't think they would help. At least I don't want to install a lot of browsers just to try this out. – inf3rno Aug 23 '16 at 19:58
  • 1
    I would be surprised if `arguments.callee` was not caught by one of the various lint tools. – user2864740 Aug 23 '16 at 20:03
  • @user2864740 Thanks! I'll remove code that uses `arguments.callee` if I can. I just realized it is not allowed by strict mode either. – inf3rno Aug 23 '16 at 20:10

2 Answers2

1

Is there an environment which supports only strict mode?

You can configure Node.js to do that, see Any way to force strict mode in node?:

node --use_strict
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

Any modern JS environment will always have strict mode in the context of certain ES6 contexts. Namely:

  • Module code is always strict mode code.
  • All parts of a ClassDeclaration or a ClassExpression are strict mode code.

Of course, the real answer here is don't use arguments.callee.caller if you want to be future proof.

But if you want to avoid the strict mode limitation, you can access Function.caller directly with named functions. This is highly discouraged, since it's a non-standard feature.

Community
  • 1
  • 1
Rahat Ahmed
  • 2,191
  • 2
  • 30
  • 40