Javascript: Difference between revisions
No edit summary |
|||
(22 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
See [https://github.com/moodboom/Reusable/tree/master/JavaScript/scrap scraps] scripts: | See [https://github.com/moodboom/Reusable/tree/master/JavaScript/scrap scraps] scripts: | ||
~/development/Reusable/JavaScript/scrap | ~/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 [https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript/5344074#5344074 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 ==== | ==== Closures ==== | ||
Line 12: | Line 59: | ||
You must use the function keyword inside another function to create a closure. | You must use the function keyword inside another function to create a closure. | ||
==== [https://github.com/moodboom/Reusable/blob/master/JavaScript/scrap/promises.js Promises] ==== | |||
==== | === Libraries === | ||
{| class="mw-collapsible mw-collapsed wikitable" | {| class="mw-collapsible mw-collapsed wikitable" | ||
! Javascript and JQuery | ! Javascript and JQuery | ||
Line 149: | Line 192: | ||
--> | --> | ||
{| class="wikitable" | {| class="wikitable" | ||
! [[Node.js]] | |||
! [[Node | |||
|} | |} | ||
{| class="mw-collapsible mw-collapsed wikitable" | {| class="mw-collapsible mw-collapsed wikitable" | ||
! Bootstrap | ! Bootstrap | ||
Line 487: | Line 453: | ||
| You have to know your display size (range) and the min and max of your data (domain), on each axis. From [https://bost.ocks.org/mike/bar/ Mike's docs]: | | You have to know your display size (range) and the min and max of your data (domain), on each axis. From [https://bost.ocks.org/mike/bar/ Mike's docs]: | ||
D3’s scales specify a mapping from data space (domain) to display space (range). | D3’s scales specify a mapping from data space (domain) to display space (range). | ||
D3’s scales can also be used to interpolate many other | D3’s scales can also be used to interpolate many other | ||
types of display-space values, such as paths, color spaces | types of display-space values, such as paths, color spaces | ||
Line 536: | Line 502: | ||
From that point on, you'll have a JavaScript date object. | From that point on, you'll have a JavaScript date object. | ||
|} | |} | ||
{| class="mw-collapsible mw-collapsed wikitable" | |||
! Accounting.js | |||
|- | |||
| Simple money library used by another bigger one (money.js) that we shouldn't need. | |||
|} | |||
{| class="mw-collapsible mw-collapsed wikitable" | |||
! auto AWS | |||
|- | |||
| | |||
* npm install -g aws-sdk | |||
* Add credentials here: C:\Users\Administrator\.aws | |||
* see existing scripts, anything is possible | |||
|} | |||
{| class="mw-collapsible mw-collapsed wikitable" | |||
! install bootstrap | |||
|- | |||
| | |||
* npm install -g grunt-cli | |||
* mkdir mysite && cd mysite | |||
* npm install bootstrap | |||
* cd node_modules/bootstrap | |||
* npm install # to actually pull down dependencies | |||
* grunt dist # builds and minifies so you're good to go! | |||
|} | |||
{| class="wikitable" | |||
! [[yarn]] alternative to npm | |||
|} | |||
<!-- | |||
=========================================================================================================================================================================================================================================================================================== | |||
--> |
Latest revision as of 19:45, 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
Javascript and JQuery | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
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 |
---|