Skip to content

Instantly share code, notes, and snippets.

@shanewholloway
Created October 31, 2020 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanewholloway/6148b4c6c2ea3f4fae1538bd35641063 to your computer and use it in GitHub Desktop.
Save shanewholloway/6148b4c6c2ea3f4fae1538bd35641063 to your computer and use it in GitHub Desktop.
Fun with class attrs and immediately invoked function expressions
class Awesome {
fn = ns => (console.log('instance function', this), this)
static s_fn = ns => (console.log('static function', this), this)
val = (ns => (console.log('instance iife', this), this))()
static s_val = (ns => (console.log('static iife', this), this))()
}
console.log('Awesome.s_val', Awesome.s_val === Awesome, Awesome.s_val)
// true
console.log('Awesome.s_fn', Awesome.s_fn)
console.log('Awesome.s_fn()', Awesome.s_fn() === Awesome, Awesome.s_fn())
// true
let obj = new Awesome()
console.log('obj.val', obj.val === obj, obj.val)
// true
console.log('obj.fn', obj.fn)
console.log('obj.fn()', obj.fn() === obj, obj.fn())
// true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment