Like any kind of apps, there are difficult issues to solve when we write JavaScript apps.
In this article, we’ll look at some solutions to common JavaScript problems.
We can replace all commas in a string with the replace
method.
For instance, we can write:
const str = 'foo,bar,baz';
const newStr = str.replace(/,/g, '-');
Then newStr
is “foo-bar-baz”
.
We can extend the Error
class with the extends
keyword.
For instance, we can write:
class SpecialError extends Error {
constructor(message) {
super(message);
this.name = 'special error';
}
}
We created a SpecialError
class which inherits from the Error
class.
We can set our own name
to distinguish it from the Error
instance.
#javascript