Why I believe this is impossible (as stated)
There aren't very many JS builtins that call a function, and even fewer that call one with arbitrary arguments. The ones that I've found are the string methods replace
and replaceAll
, the array methods every
, filter
, find
, findIndex
, findLast
, findLastIndex
, flatMap
, forEach
, map
, reduce
, reduceRight
, and some
; the the function methods apply
and call
; the global functions Reflect.apply
, setTimeout
, setInterval
, the Promise
constructor and (browser only) requestAnimationFrame
and addEventListener
, and also some weird stuff like RegExp[@@replace]
.
All the array methods are useless, as they all call functions with the signature f(item, index, array)
.
There are a few that call the function but can't return undefined. String#replace
(along with String#replaceAll
and RegExp[@@replace]
) almost work with ''.replace.bind('', /(?:)/g)
, but returns the function's result stringified. setTimeout
ignores the function's return value, but returns a uid for that particular call (ditto with setInterval
).
Likewise, the function methods apply
and call
return the return value of whatever function they're called with. Reflect.construct
can be used to trigger the Promise
constructor, but requires the arguments passed to the constructor function to be placed in an array.
Event listeners are somewhat promising, taking a function and returning undefined, but there's no way to create an object which has an event occur on it every time the function is called. window.addEventListener.bind(window, 'click')
(browser only) does work to call a function every time a click is called though.
As calling the expression with the input function has to be the last operation performed on the expression, I don't think there's any way to transform whatever non-undefined
value is returned into undefined
. And since there's no built-in function capable of both instantly calling the function a single time without arguments and returning undefined
, I don't think this challenge is possible.
(old answer, calls with nonempty args) 17 bytes
[].find.bind([,])
Try it online!
I'm pretty sure this is optimal -3 bytes thanks to Neil.
Calls the function on an array [,]
, passing undefined
to it. If the function's return value is truthy, the undefined
is returned; if it's falsy the find
call fails and returns undefined
.
()=>{}
orfunction*(){}
? \$\endgroup\$[].find
like emanresu uses) \$\endgroup\$.name
and.length
don't matter, nor does thethis
valuef
sees. As for the "returningundefined
" part, it just happened to be part of the problem I was idly attempting while bored at work; it's not for any clever reason. \$\endgroup\$