The triple dot syntax (` `) in JavaScript: rest vs. spread
2ality.com
2ality.com
Just a couple of years ago I spent a few months with people stuck in the late 2000s, technologically speaking. Their JS was your typical jQuery spaghetti from that time period. Did not have any fun explaining the new stuff, not to mention Promises, to people who were somehow convinced they knew better than me.
jQuery is still the dominant paradigm. It's used on like 80% of websites. Wordpress is also still very widely used
The existence today of e.g. react-bootstrap suggests that I'm not the only one to have had this idea.
Why would they hate on angular? Isn't just another framework
If I want/need "something more than native" today then I first ask myself again whether I _really_ need it and then maybe I will look for focused/small libraries and polyfills.
But people use promises... and keep nesting.
async.waterfall([function(callback) { callback(null, 'one', 'two'); },
function(arg1, arg2, callback) { callback(null, 'three'); },
function(arg1, callback) { callback(null, 'done'); }]);
Thus you don't need to make things like 'one' and 'two' into scoped variables that require nesting the second function into the first one so it can see them, and you can split those functions out of being inline lambdas.Or early Promise libs like coolaj86's futures which included a sequence, letting you do the same sort of thing:
var seq = Futures.sequence();
seq
.then(function(next) { setupContextObj(next, obj)})
.then(retrieveMD)
...
.then(postUrlDataToS3)
...
while also being able to make nice little future() objects you could return from functions and pass around. To be fair it's still JS and lots of third party code needed regular old callbacks so it was still easy to get excessive levels of nesting of various things that leave you with function-closing sequences like });}});return future;} or });}}).end(buf);} that could make a Lisper blush.These days it's quite a bit nicer having native Promises, a better community-wide understanding of how to use them, but also the syntactic sugar of async/await improves over the waterfall/sequence/.then&.catch chaining styles (especially when it comes to modifications where you want to use data from previous stages of the chain in later stages).
I feel like fp-ts is even more powerful for this - provided you (and your entire team / every new hire) can navigate the steep learning curve which tbh defeated me when I tried to learn it in a couple of days last week
But that’s just pedantic if you ask me. A constant source of “me smart” attitude in conversations with those unfamiliar with the concept of grammar. It’s okay to drive a car without being a qualified engine-eer.
That said, since we're discussing pedantry, I truly do mean for this to be constructive. In the US at least, most people avoid putting the "Dr." title in their name unless they're in a context where it specifically matters (such as on a course syllabus that they're teaching in university, for example). Medical doctors is an area where this is much less true. The "Dr." title can come off to many people as very pedantic unless it is specifically relevant to the situation.
Part of that is probably because historically in software development/engineering there's been a stigma against people that didn't graduate or go to university, so people feel insecure about others with a lot more education than them. It's gotten a lot better, but there's definitely still some of that happening even now.
I also want to say _thank you_ for your books and blog! I've found them quite helpful over the years. Javascript isn't my primary language so I often have to look things up, and I always click on your posts first because I know that they will:
1. Be brief and to the point (not a bunch of extra unrelated stuff to sort through)
2. Be very clear (never adds more confusion)
3. Be technically accurate
4. Thoroughly answer the question in the title
Thanks for all you do kind sir!
W.r.t. the “Dr.”: It’s interesting how much reactions differ. In Germany, people tend to think you are smart if you have a title. In the States, people tend to think you’re incapable of functioning in the real world.
I’m still experimenting. The title has worked well for me w.r.t. branding. Sometimes I mention it, sometimes I don’t.
It's not truly correct but if you think about it in this manner it actually fits reasonably well into what the result is.
0 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
1 - https://exploringjs.com/es6/ch_async.html
2 - https://github.com/microsoft/TypeScript/issues/6283#issuecom...
https://eloquentjavascript.net/11_async.html
Full book: https://eloquentjavascript.net/
– Foundations: https://exploringjs.com/impatient-js/ch_async-js.html
– Promises for async programming: https://exploringjs.com/impatient-js/ch_promises.html
– Async functions: https://exploringjs.com/impatient-js/ch_async-functions.html
– Async iteration: https://exploringjs.com/impatient-js/ch_async-iteration.html
The name "kwargs" is unimportant, I'm referring to the ** operator itself.
Edit: worth mentioning this applies in the consuming named arguments sense as well: ({arg1, arg2, ...rest}) => {}
```js
const object1 = {
field1: true,
field2: 42
};
const object2 = {
...object1,
field3: 'the meaning of life, the universe, and everything'
};
```https://es.discourse.group/t/conditionally-add-elements-to-d...
[
"foo”,
if (cond1) bar,
if (cond2) ...baz,
]
Pretty sure it was already proposed and rejected, but still. [
“foo”,
...(cond1 ? [bar] : []),
...(cond2 ? baz : []),
]
---Edit: note to self: read the link before commenting.
Q - is it just a matter of liking posts on tc39 discourse to show support?
There is no "pass by reference" in javascript, following the traditional definition, just the same as Java
Spreading introduces an overhead at runtime since the object has to be traversed to figure out what it contains before JS will know what to copy over.
Edit: just to be clear, unless you’re looping over a million items or something up there, you should not care :)
Example: the top SO answers for "find max of an array" suggest using spread [1]:
Math.max(...array)
but this runs into implementation-defined limits on the number of arguments you can pass to a function, which is often as low as 65k; now your function is needlessly limited and works differently across browsers.1: https://stackoverflow.com/questions/1669190/find-the-min-max... and https://stackoverflow.com/questions/45123468/max-of-array-in...
Yes, 20x. It only gets worse the more you do it -- it's both slower and wastes more memory, because it relies on the Iterator protocol, which actually compiles to something like this:
const foo = [];
const iter = bar[Symbol.iterator]();
while (true) {
const it = iter.next();
foo.push(it.value);
if (it.done)
break;
}
Minus some extra error handling with try/catch. Note, that all those calls to .next() every loop have to be dispatched; they can be optimized away, but V8 and JSC are very picky about when they can inline something like this. Also note that every call to .next() returns a brand new object, which means a lot of GC pressure. If you're copying an array of 20,000 items, you just created 20,000 tiny objects of garbage which all need to be GC'd.Last I asked around, everyone said escape analysis would solve this. But in the 7 years I've been writing JS, I've never seen a single engine correctly elide this object creation. Every so often I boot up d8 with its assembly dumper and confirm, that yes, it is still creating a heap object, even in simple cases, yes, even with all optimizations enabled.
I try and ban all uses of the iterator protocol (for...of, spread, some kinds of destructuring assignment) in all the high-performance code I write. It's helpful, but it's a huge battery drain. And it's because they just spec'd the feature wrong.
* this statement will self-invalidate in 180 days.
macos (or some application(s) on macos) also seems to use a unicode version of double quotes " instead of the standard ascii one. I spent a ridiculous amount of time debugging a line in a shell script that had the unicode quotes instead of "normal" ones and broke in ways that were horrible to figure out. Eventually I discovered that he had composed the line in Slack and pasted it in. Definitely something to watch out for!
For reference:
TextEdit - Preferences - Disable 'Smart Quotes' and 'Smart Dashes'
I'd put my money on an automatic HN thing, it massages titles a fair amount.