On the web browser side, I don't recommend using ES6 yet, without any kind of fallback. Internet Explorer 11 is still used, as are devices on older iOS versions. (without counting people using the default browser on pre-Lollipop Android)
Overview of JavaScript ES6 features
121–130 of 250 posts
Re: Overview of JavaScript ES6 features
#122Earlier quoted context omitted.
Maybe in ES9 we will have immut x = {}; x.foo = 'it doesn't work! :)' At the moment, we can use these libs to achieve it - https://github.com/rtfeldman/seamless-immutable - https://github.com/facebook/immutable-js
We don't need any libraries to solve this issue (please don't bring in libraries to do weird stuff like this; that's a dependency that you'll be stuck with forever over your entire codebase for essentially zero reason IMO). Just use `const` + `Object.freeze()`; it'll get you 99.9995% of exactly what you want.
Re: Overview of JavaScript ES6 features
#123Can someone smarter than me (not a high bar) explain why they didn't just fix var, rather than introducing let?
Re: Overview of JavaScript ES6 features
#124I'm surprised by the state of const/let nowadays. The well-known good practice: use const by default; use let when it's needed. At the release of ES6, it was the way to go. But everyday I notice libraries—and some really famous— that use let everywhere in their docs, or some really influent developers from Google or Facebook who share samples of code on Twitter using let when it's not needed [1]. I don't know why. Se…
It also doesn't help that let is 3 letters versus const is 5; programmers if anything will default to the faster to type option.
Re: Overview of JavaScript ES6 features
#125Earlier quoted context omitted.
Ah, I see, good point. And how would you handle cases (that I end up using quite often) such as : arr.map(...).filter(...).forEach(...) which allows me to iterate over the filtered result? One would assign the result of filter to a variable and call for...of on that? EDIT: Also, I never saw any mention of for...of working for object literals (à la `for (let [key, value] of obj`), I suppose that's out of scope, correc…
> arr.map(...).filter(...).forEach(...) which allows me to iterate over the filtered result? Just like that. It would be pretty nice to have generic filter/map that can work on arbitrary iterables, but we don't have that right now. :( > Also, I never saw any mention of for...of working for object literals (à la `for (let [key, value] of obj`) for (let [key, value] of Object.entries(obj)) { } it's not in "ES6"/ES2015,…
function* map(iterator, mapper) {
for (const elem of iterator) {
yield mapper(elem);
}
}
function* filter(iterator, filterer) {
for (const elem of iterator) {
if (filterer(elem)) {
yield elem;
}
}
}
function forEach(iterator, eacher) {
for (const elem of iterator) {
eacher(iterator);
}
}
function reduce(iterator, reducer, initial) {
let current = initial;
for (const elem of iterator) {
current = reducer(current, elem);
}
return current;
}Re: Overview of JavaScript ES6 features
#126I'm surprised by the state of const/let nowadays. The well-known good practice: use const by default; use let when it's needed. At the release of ES6, it was the way to go. But everyday I notice libraries—and some really famous— that use let everywhere in their docs, or some really influent developers from Google or Facebook who share samples of code on Twitter using let when it's not needed [1]. I don't know why. Se…
Re: Overview of JavaScript ES6 features
#127I'm surprised by the state of const/let nowadays. The well-known good practice: use const by default; use let when it's needed. At the release of ES6, it was the way to go. But everyday I notice libraries—and some really famous— that use let everywhere in their docs, or some really influent developers from Google or Facebook who share samples of code on Twitter using let when it's not needed [1]. I don't know why. Se…
Re: Overview of JavaScript ES6 features
#128Earlier quoted context omitted.
Common misconception, const only disallows reassignment. For instance, const number = 1337; number = 10; // fails But objects/arrays are not immutable in js, so you can do this: const person = { name: 'Dude' }; person.name = 'Dudette'; Which is perfectly valid. If you want full immutability, i recommend you check out https://facebook.github.io/immutable-js/ , been running it in production, a real pleasure to work wit…
Any popular "deep freeze" utilities around?
function deepFreeze(obj) {
const out = Object.freeze(obj);
Object.keys(out).forEach(key => {
if (typeof out[key] === 'object') {
out[key] = Object.freeze(out[key]);
}
});
return out;
}Re: Overview of JavaScript ES6 features
#129Earlier quoted context omitted.
I agree that object destructuring makes the code far less readable. Array destructuring however is easy for anyone to grok.
It depends I think. For example, in my opinion const Header = ({ children, iconName, iconSize, title }) => { ... }; is more readable than const Header = (props) => { ... };
Re: Overview of JavaScript ES6 features
#130Earlier quoted context omitted.
Let is a game changer imo. In the world of tens of dependencies, knowing that your variables are scope restricted reduces your cognitive load. It's one less thing that can go wrong. I agree with you on classes, but that's probably more because I've never been a big fan of OO in practice. It doesn't really seem to have seen much use in the greater js ecosystem though, unlike let/const.
Honest question here --- What is the difference between let and global variables? There are hundreds of articles written about the doom associated with PHP globals, but let appears to be universally lauded. I must be missing something, but I can't tell where.
var foo;
var bar;
{
let foo = "hello";
var bar = "world";
}
console.log(foo);
console.log(bar);
This produces: undefined
world
The reason being that the `let` statement restricted that variable to the block it was in (defined by the { and }). `var` declares the variable globally, allowing it to be accessed outside of the {}.We prefer now to use `let` and `const` over `var` because it doesn't pollute the global namespace. With the asynchronous nature of Javascript, it's theoretically possible for you to declare a variable with `var`, assign it a value, then immediately use that value and find that it's different than what you expected because of another function using the same variable name. This isn't possible with `let`.