Created
November 4, 2019 21:17
-
-
Save necccc/ca282f807a71930b10ea54dc2e45705f to your computer and use it in GitHub Desktop.
Gist for "Upcoming ESNext features - Part 2"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
::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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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') | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// IIF | |
(() => { /* code */ })(); | |
// IIAF | |
(async function () { | |
/* await code */ | |
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const homeworld = person.homeworld.name || 'unknown' | |
// Uncaught TypeError: Cannot read property 'name' of undefined | |
const homeworld = person.homeworld && person.homeworld.name || 'unknown' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
obj?.prop // optional static property access | |
obj?.[expr] // optional dynamic property access | |
func?.(...args) // optional function or method call |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const homeworld = person.homeworld?.name || 'unknown' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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