Alternative syntax using `this`?
See original GitHub issueAn alternate, more “method”-y API would be to use this
as the state variable, which would mean you could provide a simple record instead of a function of state:
const initialState = { count: 0 };
const methods = {
reset() {
return initialState;
},
increment() {
this.count++;
},
decrement() {
this.count--;
},
};
Another advantage is that VSCode gives this
special emphasis and color, so it’s easy to tell at a glance where state access and “mutation” is happening.
Downsides of this approach:
- Can’t use arrow functions to define methods
- Can’t decide to use a shorter name for the state variable, e.g.
s
- Can’t compute variables from state that will be in scope for all methods, e.g.:
const methods = state => { const { x, y, z } = state; return { // ... method definitions } };
I’m curious if others have any opinions about this.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Alternative syntax for control structures - Manual - PHP
PHP offers an alternative syntax for some of its control structures; namely, if , while , for , foreach , and switch ....
Read more >php - When, where & why should I use the "alternative syntax ...
I typically only use it when mixing HTML and PHP, so just for the sake of argument, here is an example to compare...
Read more >Alternative Syntax - Ruby for Beginners
Strings. First, Ruby has an alternative syntax for defining strings that goes like so: %[any-character]The actual string[the same ...
Read more >Alternate PHP Syntax for View Files : CodeIgniter User Guide
Alternate PHP Syntax for View Files. If you do not utilize CodeIgniter's template engine, you'll be using pure PHP in your View files....
Read more >Alternative syntax for control structures - PHP - Webdev
PHP offers an alternative syntax for some of its control structures; namely, if, while, for, foreach, and switch. In each case, the basic...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I like it as-is (without
this
), partially because we use a lint that disallowsthis
outside class bodies, and partially becausethis
is a piece of JS magic that always seems suspicious.Yeah,
this
would always be bound, because we wouldn’t be using classes, just generating a bunch of functions which come pre-bound to their context.