Exposes the unexposed constructors like Generator, GeneratorFunction, AsyncFunction


Keywords
Generator, GeneratorFunction, AsyncFunction, hidden, unexposed, constructor, constructors
License
MIT
Install
npm install unexposed@0.0.7

Documentation

unexposed

npm install
Travis Build Status CircleCI Build Status Build Status CircleCI Dependencies Status Known Vulnerabilities Downloads License

JavaScript has some global objects that are not really exposed as global objects.

It's hard to even tell what they are. Not global objects? Well, not in a sense that they're local objects. I call them unexposed objects.

They are usually documented under "global objects" in documentation like MDN because it's hard to classify them.

For example, we have Function in JS Reference / Global Objects:

that explains what new Function() does.

We also have AsyncFunction in the same JS Reference / Global Objects:

that explains what new AsyncFunction() does, but it also contains an interesting note:

"Note that AsyncFunction is not a global object."

In the Node REPL we can create a function and inspect it:

> x = function () {};
[Function: x]
> Function
[Function: Function]

but it we do the same with an async function then we get a strange error:

> x = async function () {};
[AsyncFunction: x]
> AsyncFunction
ReferenceError: AsyncFunction is not defined

We can easily instantiate Function with:

x = new Function();

But how can we use new AsyncFunction() syntax that is explained in the documentation if AsyncFunction is not defined? We need to use hacks like this:

x = new (Object.getPrototypeOf(async function () {}).constructor)();

which is unreadable and error prone.

The purpose of this module is to expose the unexposed. What you do with them is up to you.

Returned Objects

Currently this modules exposes:

  • AsyncFunction
  • GeneratorFunction

Issues

For any bug reports or feature requests please post an issue on GitHub.

Author

Rafał Pocztarski
Follow on GitHub Follow on Twitter
Follow on Stack Exchange

License

MIT License (Expat). See LICENSE.md for details.