TIL

Javascript

Forward Declaration (2019-08-20)

Many linters check for functions to exist before they're referenced. With circular calling, you need to declare a function before it's defined. TIL this is called forward declaration.

var bind = function bindForward() {};
 
function ajax(url) {
  load(url).then(bind);
}
 
bind = function bind() {
  $("a").click(ajax);
}

Destructuring (2019-08-08)

When destructuring, JS only assigns the deepest nested keys:

const req = { query: { person: { first: "Chris", last: "Hallberg" } } };
 
const { query: { person: { first, last } } } = req;
 
console.log(query);  // undefined 
console.log(person); // undefined 
console.log(first);  // "Chris" 
console.log(last);   // "Hallberg 

You can also assign names that don't match the keys you're grabbing. Just be careful with your brackets.

const req = { query: { data: "apples" } };
 
var { query: data } = req; // rename query to data 
console.log(data); // { data: "apples" } 
 
var { query: { data } } = req; // destruct data from query 
console.log(data); // apples 

Warning: In browsers, name seems to be assigned to "[object Object]" somewhere along the line, so be careful.


Thank you, Stefan Judis for the idea and encouragement.

Sexy Tooltip