Skip to content

Instantly share code, notes, and snippets.

@necccc
Created November 4, 2019 21:17
Show Gist options
  • Save necccc/ca282f807a71930b10ea54dc2e45705f to your computer and use it in GitHub Desktop.
Save necccc/ca282f807a71930b10ea54dc2e45705f to your computer and use it in GitHub Desktop.
Gist for "Upcoming ESNext features - Part 2"
::instance.method
// returns a "method" function, bound by context to the "instance"
// so inside "method" the "this" will point to "instance"
variable::method(arg1, arg2, ...)
// runs the "method" function with "variable" as context, and optional arguments
// so inside "method" the "this" will point to "variable"
// pass in `this` to a function
const elements = document.querySelectorAll("div")
const filterCollectionByClass = function (classname) {
return Array.from(this).filter(item => item.className.indexOf(classname) >= 0)
}
// current solution
filterCollectionByClass.call(elements, 'some-classname')
// run "filterCollectionByClass" with the "elements" collection as "this"
// using the Bind Operator
elements::filterCollectionByClass('some-classname')
// use `console.log` as a function, bound to `console`
// current solution
[ 1,2,3,4,5 ].forEach(console.log.bind(console))
// using the Bind Operator
[ 1,2,3,4,5 ].forEach(::console.log)
// reaching a module, based on the visitor's state
const text = await import(`languages/${userLang}/module.js`)
// code splitting
import { criticalPart } from 'critical-components'; // critical stuff loads sync
await criticalPart(); // do render
const lazy = await import('lazy-components'); // load the rest async
// debugging, or import decision based on server environment
if (process.env.NODE_ENV !== 'production') {
await import('framework-debugger')
}
// IIF
(() => { /* code */ })();
// IIAF
(async function () {
/* await code */
})()
const homeworld = person.homeworld.name || 'unknown'
// Uncaught TypeError: Cannot read property 'name' of undefined
const homeworld = person.homeworld && person.homeworld.name || 'unknown'
obj?.prop // optional static property access
obj?.[expr] // optional dynamic property access
func?.(...args) // optional function or method call
const homeworld = person.homeworld?.name || 'unknown'
// static import
import foo from 'foo'
// dynamic import, returns a promise, can be awaited
const bar = await import('bar')
// computed dynamic import
const lang = 'en_us'
const text = await import(`languages/${lang}/text.js`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment