Javascript: Difference between revisions
No edit summary |
|||
Line 60: | Line 60: | ||
==== [https://github.com/moodboom/Reusable/blob/master/JavaScript/scrap/promises.js Promises] ==== | ==== [https://github.com/moodboom/Reusable/blob/master/JavaScript/scrap/promises.js Promises] ==== | ||
=== Libraries === | === Libraries === |
Revision as of 19:32, 24 February 2023
Basics
See scraps scripts:
~/development/Reusable/JavaScript/scrap
ES6
ES6 is required learning!
Scope
myglobal = 1; // global scope, defined outside of any scope var myvar = 1; // local scope, cacades into closures and blocks; OLDSKOOL, avoid let myblockvar = 3; // block scope const myconst = 4; // const, has block scope
Sorting
You'd think the sort() call's comparison function would be a "less than" check, like in C++. NOPE. You MUST return -1 or 1, not a boolean result. Wtf were they thinking... Working example:
data.sort(( a, b ) => { // DO NOT SIMPLY RETURN a.value < b.value, you will gnash and cry bitter tears... return ( a.value < b.value ) ? 1 : -1; });
Array find-or-fail
The old stupid anti-pattern that far too many rookies jump right in to:
if find then find else fail.
There are a couple ways to properly find-or-fail, here's one, I have a better one that I can't remember atm, doh...
const targetType = "selected"; myArray.find( item => item.type === targetType )?.description || 'unknown'
Immutability
- Don't think that const gives you a const variable or object. Only the variable binding is const, ie it can't be reassigned.
- myVar.freeze() will make your var shallowly immutable - but what's the point of that?
- Still waiting on a deep immutable standard solution. Ridiculous. As a workaround, use deep copies (also hard to do, see next item).
Deep Copy
Assignment of a new object to an existing one makes the new object a reference to the existing:
var a = { "value": 3 } var b = a; a.value = 4; // b.value will also now = 4.
Obviously, often (for better immutability for example), we want a copy. It's hard to believe but deep copies are hard to accomplish out of the box in silly Javascript. According to this guy's benchmark, this works well:
var newAllocation = JSON.parse(JSON.stringify(deepObject))
For futureproofing and convenience, use my deepCopy() function wrapper, in [at.js].
Closures
If you declare a function within another function, then the local variables can remain accessible after returning from the function you called.
The scope of the internal local variables is the lifetime of the function. So if you have a function, you have its local variables too.
You must use the function keyword inside another function to create a closure.
Promises
Libraries
Node.js |
---|
Bootstrap | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
D3 | ||||
---|---|---|---|---|
|
Vega-lite |
---|
Moment.js |
---|
Make sure to specify a format when using a STRING SOURCE, eg:
moment(myinputdate, "HH:mm:ss MM-DD-YYYY") From that point on, you'll have a JavaScript date object. |
Accounting.js |
---|
Simple money library used by another bigger one (money.js) that we shouldn't need. |
auto AWS |
---|
|
install bootstrap |
---|
|
yarn alternative to npm |
---|