Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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
You can’t perform that action at this time.