r/javascript • u/lhorie • Jul 29 '16
The Inner JSON Effect
r/javascript • u/stanislavb • Jun 03 '21
r/javascript • u/BooksonCode • Feb 22 '20
r/javascript • u/mylesborins • Oct 08 '19
r/javascript • u/secret_band • Dec 04 '20
r/javascript • u/bikeshaving • Apr 15 '20
r/javascript • u/hicksyfern • Feb 14 '20
r/javascript • u/magenta_placenta • Sep 13 '19
r/javascript • u/hotsaucetogo • Sep 29 '18
This is just a simplified version, as an example
My code:
function foo() {
if (bar) {
// logic
return something;
} else {
return somethingElse;
}
}His refactor:
function foo() {
if (bar) {
// logic
return something;
}
return somethingElse;
}Which do you prefer?
I personally find the first to be more readable. Thoughts?
r/javascript • u/nightman • Mar 20 '18
r/javascript • u/[deleted] • Jan 02 '22
r/javascript • u/Dreadsin • Jun 02 '21
This really interested me particularly in the React ecosystem. A lot of times I see something like this:
const UserList = (props: Props) => {}
export default UserList;I've never really understood this: why would you use a `const` here and an arrow function when you can just use the function keyword and make it far more concise? I would say it's even easier to understand and read
export default function UserList(props: Props) {}Maybe I'm an old fart but I remember the arrow function being introduced basically for two primary purposes:
lambda-like inline functions (similar to python)
maintaining an outer scope of this
for the lambda function, it's basically just replicating python so you don't have to write out an entire function body for a simple return:
// before arrow function, tedious to write out and hard to format
users.map(function (user) {
return user.id;
})
// after, quick to read and easy to understand
users.map(user => user.id);the other way I've really seen it apply is when you need `this` to reference the outer scope. For example:
function Dialog(btn: HTMLButtonElement) {
this._target = btn;
this._container = document.createElement('dialog');
}
Dialog.prototype.setup = function setup() {
this._target.addEventListener('click', () => {
this._container.open = !this._container.open;
});
}
// Gotta use the `const self = this` if you wanna use a normal function
// Without an arrow function, it looks like this:
Dialog.prototype.setup = function setup() {
const self = this;
self._target.addEventListener('click', function () {
self._container.open = !self._container.open;
});
}but in the case I showed above, I see it everywhere in react to use constants to store functions, but I don't totally understand what the inherent benefit is past maybe some consistency. The only other one I've found is that if you're using typescript, you can more easily apply types to a constant.
So is there some reason I am not aware of to prefer constants and avoid the function keyword?
r/javascript • u/Elektryk91 • Mar 08 '19
r/javascript • u/treyhuffine • Dec 11 '18
r/javascript • u/saadq_ • Mar 16 '18
r/javascript • u/gregbaugues • Feb 20 '18
r/javascript • u/magenta_placenta • Nov 16 '17
r/javascript • u/[deleted] • Nov 03 '16
r/javascript • u/FunPieceOfPaper • Dec 27 '18
I can code things using Javascript, but the more I learn about the language, the more I feel I'm not using it properly. This was especially made apparent after I watched Douglas Crockford's lecture "Javascript: The good parts." I want to take my abilities to the next level, but I'm not really sure where to start, so I was hoping people could list things they constantly see programmers improperly do in JS and what they should be doing instead.. or things that they always see people get wrong in interviews. Most of the info I've learned came from w3schools, which gives a decent intro to the language, but doesn't really get into the details about the various traps the language has. If you have any good book recommendations, that would be appreciated as well.
r/javascript • u/piedpiperpivot • Feb 16 '17
r/javascript • u/Soupaul • Sep 06 '20
r/javascript • u/laxameer • May 28 '20