1

The first one works as expected:

var f1 = Object.getOwnPropertyNames(Function)
    .forEach(function(element) {
         console.log (typeof Function[element]);
    });  //  --> number, string, function

The second one outputs an error message:

var f2 = Object.getOwnPropertyNames(Function.prototype)
    .forEach(function(element) {
        console.log (typeof Function.prototype[element]);
});

TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode

How can I bypass that?

Edit: Current Workaround

var forbiddenOnStrictMode = ['caller', 'callee', 'arguments'];

var f2 = Object.getOwnPropertyNames(Function.prototype)
    .forEach(function(element) {
        if (forbiddenOnStrictMode.indexOf(element) == -1)
        console.log (typeof Function.prototype[element]);
});

Is it possible to say node-compiler ignore the strict mode for a while?

  • What is the context for this code? What is forcing it into strict mode in the first place? Is it in a class method definition? As far as I know, you can't "undefine" strict mode, but perhaps you could move the code somewhere else that is not in strict mode and call it. – jfriend00 Dec 26 '17 at 16:27
  • I simple want to generate a documentation for JS-Function-methods (if `Function.prototype[whatever] == 'function'` then create a heading in the document. But e.g.Function.prototype.caller is not accessible on strict mode (=default mode) and therefore you can not access an existing method, because compiler says NO. –  Dec 26 '17 at 16:32
  • The question I'm trying to ask is what is this code inside of and why is it in strict mode? node.js code is not by default in strict mode so something you are doing is making it be in strict mode. I want to know why it's in strict mode. What context of the code makes it be in strict mode? You can perhaps move this code outside of strict mode and call it. – jfriend00 Dec 26 '17 at 16:34
  • You can try the following in your browser's console: `Object.getOwnPropertyNames(Function.prototype).forEach(function(element) {console.log (typeof Function.prototype[element]);})` Copy+paste+Enter –  Dec 26 '17 at 16:41
  • You're not answering my question - I don't understand why. The browser console is a non-standard place so I'm not going to try to solve or explain anything in the console. If you have real code you want help with a real problem on, then show the context of the real code. Otherwise, I'll just move on to a real problem to solve somewhere else. – jfriend00 Dec 26 '17 at 16:50
  • That's exactly the real code above, in node or browser environments you will get the same result, even if you did not add `"use strict";` etc, that's the standard behavior in most recents node versions and also in browsers. There must be now a way to say node to deactivate strict mode. –  Dec 26 '17 at 16:56
  • So, is this code inside a node.js module? What version of node.js? Browsers do NOT default to strict mode except inside certain new ES6 features and they can't for compatibility reasons. As far as I know, there is NO way to turn off strict mode once it is enabled. You'd have to define the code in a place where strict mode was not enabled and then you could call it from a strict mode function. – jfriend00 Dec 26 '17 at 17:06
  • that is a file e.g. `myFileName.js` and you run that with `node myFileName.js` in your terminal. –  Dec 26 '17 at 17:09
  • My testing in node v8.8.1 shows that a simple file run like you said is NOT in strict mode. I think what you're seeing is a somewhat misleading error message. It's not the code you're running that is in strict mode. You're trying to access something on `Function.prototype` which itself is marked as being defined in strict mode. – jfriend00 Dec 26 '17 at 17:14

1 Answers1

0

My testing in node v8.8.1 shows that a simple file run like you said is NOT in strict mode. I think what you're seeing is a somewhat misleading error message. It's not the code you're running that is in strict mode. You're trying to access something on Function.prototype which itself is marked as being defined in strict mode and therefore the interpreter is refusing to let you access those properties on that object.

Is it possible to say node-compiler ignore the strict mode for a while?

No, there is not a way to do that. But that is not actually your issue. You can test whether your own code is in strict mode with this:

const isStrict = (function() { return !this; })();
console.log("strict mode", isStrict);

You will find that a simple file run by node.js is not in strict mode. Your issue is that you're trying to access a prototype that is marked by node.js as a strict mode definition. That strict mode definition is built into the JS implementation. It doesn't come from a strict mode in your code. I am not aware of any way to change that. I think you will have to go with your work-around.

jfriend00
  • 683,504
  • 96
  • 985
  • 979