Kje*_*lly 3 javascript ternary ecmascript-6
我多次遇到这种情况,我根据二元情况调用函数,其中三元可以正常工作:
buttonPressed ? files.audio.current.play() : files.audio.current.stop()
Run Code Online (Sandbox Code Playgroud)
但是看到所有额外浪费的代码?有没有办法做更像的事情:
files.audio.current.(buttonPressed ? play : stop)()
Run Code Online (Sandbox Code Playgroud)
使用括号表示法,并将函数名称作为字符串从trinary返回:
files.audio.current[buttonPressed ? 'play' : 'stop']()
Run Code Online (Sandbox Code Playgroud)