Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class Extends Function: Extra Newness #78

Closed
joedski opened this issue Feb 1, 2018 · 2 comments
Closed

Class Extends Function: Extra Newness #78

joedski opened this issue Feb 1, 2018 · 2 comments
Assignees
Labels
new-example A proposal of the new example released Issues and PRs released by semantic-release

Comments

@joedski
Copy link

joedski commented Feb 1, 2018

I present this as an oddity for your amusement.

class Foo extends Function {
  constructor(val) {
    super();
    this.prototype.val = val;
  }
}

new new Foo(':D')().val // => ':D'

Constructors in Javascript are of course just functions with some special treatment. by extending Function using the ES6 class syntax you create a class that, when instantiated, is now a function, which you can then additionally instantiate.

While not exhaustively tested, I believe the last JS statement can be analyzed thus:

(new (new Foo(':D'))()).val
(new newFooInstance()).val
veryNewFooInstance.val
':D'

As a tiny addendum, doing new Function('return "bar";') of course creates a function with the body return "bar";. Since super() in the constructor of our Foo class is calling Function's constructor, it should come as no surprise now to see that we can additionally manipulate things in there.

class Foo extends Function {
  constructor(val) {
    super(`
      this.val = arguments[0];
    `);
    this.prototype.val = val;
  }
}

var foo = new new Foo(':D')('D:');
foo.val // -> 'D:'
delete foo.val; // remove the instance prop 'val', deferring back to the prototype's 'val'.
foo.val // -> ':D'
@denysdovhan denysdovhan added the new-example A proposal of the new example label Feb 5, 2021
@denysdovhan denysdovhan self-assigned this Feb 5, 2021
@denysdovhan
Copy link
Owner

denysdovhan commented Feb 6, 2021

Added to the list and will be released soon! Thanks a lot!

github-actions bot pushed a commit that referenced this issue Feb 6, 2021
# [1.17.0](v1.16.0...v1.17.0) (2021-02-06)

### Bug Fixes

* Lint markdown files ([aa26445](aa26445))

### Features

* Add A stringified string example. Close [#113](#113) ([de081e4](de081e4))
* Add alert from hell. Close [#55](#55) ([1d973b9](1d973b9))
* Add an example with arguments. Close [#35](#35) ([1289b0c](1289b0c))
* Add an infinite timeout example. Close [#61](#61) ([f356deb](f356deb))
* Add comparison table. Close [#107](#107) ([c40b0df](c40b0df))
* Add double dot example. Close [#124](#124) ([597b5ee](597b5ee))
* Add example with Math.min and Math.max. Close [#23](#23). ([ad120b2](ad120b2))
* Add Extra Newness. Close [#78](#78) ([81316dc](81316dc))
* Add Split a string by a space. Close [#171](#171) ([827da42](827da42))
* Add Why you should use semicolons. Close [#112](#112) ([7488b34](7488b34))
* Non-strict comparison of a number to true. Close [#135](#135) ([0242818](0242818))
@github-actions
Copy link

github-actions bot commented Feb 6, 2021

🎉 This issue has been resolved in version 1.17.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-example A proposal of the new example released Issues and PRs released by semantic-release
Projects
None yet
Development

No branches or pull requests

2 participants