I was writing a program that uses javascript and creates inheritance in a way.
If the user types in:
x.isSingleLine()
and it does not exist, it crashes Javascript. I was hoping there was a way to do something like:
x.isSingleLine() || "Doesnt Exist";
to in a way have it do a soft error or some sort of alert. I didnt want to have to encapsulate the object in a try-catch block.
Is there a way this can be acplished?
In the statement: x.isSingleLine() || "Doesnt Exist"; it will crash and not do the OR part, so this is not the answer apparently.
I was writing a program that uses javascript and creates inheritance in a way.
If the user types in:
x.isSingleLine()
and it does not exist, it crashes Javascript. I was hoping there was a way to do something like:
x.isSingleLine() || "Doesnt Exist";
to in a way have it do a soft error or some sort of alert. I didnt want to have to encapsulate the object in a try-catch block.
Is there a way this can be acplished?
In the statement: x.isSingleLine() || "Doesnt Exist"; it will crash and not do the OR part, so this is not the answer apparently.
Share Improve this question edited Jul 22, 2013 at 13:30 Paul D. Waite 99k57 gold badges203 silver badges271 bronze badges asked Jul 22, 2013 at 13:25 FallenreaperFallenreaper 10.7k15 gold badges75 silver badges140 bronze badges 1- 4 "it crashes Javascript" - it's more accurate to say that JavaScript throws an error. – Paul D. Waite Commented Jul 22, 2013 at 13:29
5 Answers
Reset to default 6How about that?
x.isSingleLine ? x.isSingleLine() : console.log("Doesnt Exist");
Edit:
As suggested by @FelixKling if there's a chance that isSingleLine
exists but is not a function you should instead use:
(x.isSingleLine && x.isSingleLine instanceof Function) ? x.isSingleLine() : console.log("Doesnt Exist");
You need to check if it exists before calling it.
var result = x.isSingleLine ? x.isSingleLine() : "Doesn't Exist";
As far as I know from the top of my head, you can use a try-catch block for that:
try {
x.isSingleLine();
} catch (e) {
// Something happened
alert("isSingleLine didn't quite work out.");
}
Play around with it in this fiddle
Update I now noticed that you don't want to encapsulate in a try-catch block. Using a try-catch block though can even handle errors inside the isSingleLine() method as shown in the updated fiddle.
(x.isSingleLine || function(){
console.log("Doesn't Exist");
})()
Optional chaining works for possibly non-existent methods
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining#optional_chaining_with_function_calls
x.isSingleLine?.()
You can use nullish coalescing to show an alert
x.isSingleLine?.() ?? console.log("Doesnt Exist")
However note that the alert will also be shown if isSingleLine
evaluates to either null
or undefined
.
Additionally, as pointed out by other ments, this will still fail if isSingleLine
is defined but is not a function.
I am aware that this is an old question. I just somehow ended up here and did not see this answer, so I posted it.