Skip to content

Instantly share code, notes, and snippets.

@daneden
Created February 9, 2019 03:11
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 daneden/a67d120529826ae9f665cd1d41196ddf to your computer and use it in GitHub Desktop.
Save daneden/a67d120529826ae9f665cd1d41196ddf to your computer and use it in GitHub Desktop.
// I learned in this post (https://overreacted.io/react-as-a-ui-runtime/)
// that arguments passed to a function run before the function itself:
/**
* // This runs second
* outerFunction(
* // This runs first
* innerFunction()
* )
*/
// This makes sense. But I wondered what it looked like in practice.
// I'm probably not testing it the right way, but I found something interesting.
const obj = {
fn: (arg) => {
// fn() is basically just an identity function that returns its own argument
return arg
}
}
// Store the function so we can reuse it later
const fn = obj.fn
// These three results make sense
obj.fn(1 + 1) // => 2
obj.fn(obj.foo = 'bar') // => 'bar'
obj.fn(obj.fn = () => 1) // => () => 1
// This is where it gets weird.
// `delete obj.fn` doesn't appear to run first, since `1` is returned,
// which is the return value of the previous function.
obj.fn(delete obj.fn) // => 1
// Let's put the original function back in place
obj.fn = fn
obj.fn(delete obj.fn) // => true
obj.fn(obj.fn = () => 1) // error! obj.fn is not a function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment