I'm trying to figure out how to convert this syntax into ES5 syntax.
moveUp() {
this.row > 0 ? this.row -= 1 : this.row = 0
}
The only part of that that isn't ES5 is the fact it's a method declaration. I'm assuming this is in an object literal, like this:
const obj = {
moveUp() {
this.row > 0 ? this.row -= 1 : this.row = 0;
}
};
If so, the near-equivalent in ES5 is to use a function expression in a property initializer:
const obj = {
moveUp: function() {
this.row > 0 ? this.row -= 1 : this.row = 0;
}
};
Side note: I recommend not using the conditional operator purely for side-effects like that. It's an if/else thing, use if
/ else
(in ES5 or ES2015+):
const obj = {
moveUp: function() {
if (this.row > 0) {
--this.row;
} else {
this.row = 0;
}
}
};
It's also questionable whether you need the else
at all. Presumably nothing assigns a negative number to this.row
, so you could probably just do this:
const obj = {
moveUp: function() {
if (this.row > 0) {
--this.row;
}
}
};
If you are looking an "easier syntax" add the this.row = 0
to the else part of your if statement
moveUp(){
if (this.row > 0){
this.row -= 1
} else {
this.row = 0
}
}
You can learn more about if statements here https://www.w3schools.com/js/js_if_else.asp
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.