multiple actions within short hand javascript
-
01-07-2021 - |
Question
Hi I am wondering if there is a good way to run multiple functions or methods if a condition is met within short-hand javascript.
I have tried this, but doesn't work:
!gameView?launchFull(); alert('action 2'):returnView();
La solution
if there is a good way
No.
The ternary operator is a good way to do a simple "If A x = y ELSE x = z". Trying to go beyond that is a good way to create an unreadable mess.
Use a proper if { } else { }
.
Readability is far more valuable then shortness.
Autres conseils
Can you do it? Yes.
Working example
var x = true;
!x?(alert('true 1'),alert('true 2')):(alert('false 1'),alert('false 2'));
Note brackets around the sections.
But, should you do it? no.
you can do like this.
function m() {alert("i am M");}
function k() {alert("i am K");}
function l() {alert("i am L");}
m.call();
var func = 1===1 ? l : k;
func.call();
Func will work as a delegate and when the call is made it will have a function associated with the variable