Skip to content

Commit

Permalink
feat: Add Extra Newness. Close #78
Browse files Browse the repository at this point in the history
  • Loading branch information
denysdovhan committed Feb 6, 2021
1 parent 597b5ee commit 81316dc
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Currently, there are these translations of **wtfjs**:
- [An `alert` from hell](#an-alert-from-hell)
- [An infinite timeout](#an-infinite-timeout)
- [Double dot](#double-dot)
- [Extra Newness](#extra-newness)
- [πŸ“š Other resources](#-other-resources)
- [πŸŽ“ License](#-license)

Expand Down Expand Up @@ -1920,6 +1921,54 @@ You must always use parenthesis or an addition dot to make such expression valid
- [Usage of toString in JavaScript](https://stackoverflow.com/questions/6853865/usage-of-tostring-in-javascript/6853910#6853910) on StackOverflow
- [Why does 10..toString() work, but 10.toString() does not?](https://stackoverflow.com/questions/13149282/why-does-10-tostring-work-but-10-tostring-does-not/13149301#13149301)

## Extra Newness

I present this as an oddity for your amusement.

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

new new Foo(':D')().val // -> ':D'
```

### πŸ’‘ Explanation:

Constructors in JavaScript are just functions with some special treatment. By extending Function using the 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 statement can be analyzed thus:

```js
(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.

```js
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'
```

- [Class Extends Function: Extra Newness](https://github.com/denysdovhan/wtfjs/issues/78)

# πŸ“š Other resources

- [wtfjs.com](http://wtfjs.com/) β€” a collection of those very special irregularities, inconsistencies and just plain painfully unintuitive moments for the language of the web.
Expand Down

0 comments on commit 81316dc

Please sign in to comment.