Categories
JavaScript Answers

How to add the JavaScript equivalent of the Python pass statement that does nothing?

Sometimes, we want to add the JavaScript equivalent of the Python pass statement that does nothing.

In this article, we’ll look at how to add the JavaScript equivalent of the Python pass statement that does nothing.

How to add the JavaScript equivalent of the Python pass statement that does nothing?

To add the JavaScript equivalent of the Python pass statement that does nothing, we can add an empty block.

For instance, we write

if (condition) {
}

to add an empty if block which does nothing.

Conclusion

To add the JavaScript equivalent of the Python pass statement that does nothing, we can add an empty block.

Categories
JavaScript Answers

How to extend error in JavaScript with ES6 syntax?

Sometimes, we want to extend error in JavaScript with ES6 syntax.

In this article, we’ll look at how to extend error in JavaScript with ES6 syntax.

How to extend error in JavaScript with ES6 syntax?

To extend error in JavaScript with ES6 syntax, we can use the extends keyword.

For instance, we write

class ExtendableError extends Error {
  constructor(message) {
    super();
    this.message = message;
    this.stack = new Error().stack;
    this.name = this.constructor.name;
  }
}

to create the ExtendableError class which inherits from the Error class.

In the constructor, we call super to initialize the parent constructor.

Then we add the instance properties for the constructor instance with the next 3 lines.

Conclusion

To extend error in JavaScript with ES6 syntax, we can use the extends keyword.

Categories
JavaScript Answers

How to get objects value if its name contains dots with JavaScript?

Sometimes, we want to get objects value if its name contains dots with JavaScript.

In this article, we’ll look at how to get objects value if its name contains dots with JavaScript.

How to get objects value if its name contains dots with JavaScript?

To get objects value if its name contains dots with JavaScript, we can use square brackets.

For instance, we write

const smth = myData.list[0]["points.bean.pointsBase"][0].time;

to put "points.bean.pointsBase" in square brackets to access this property in the myData object.

Conclusion

To get objects value if its name contains dots with JavaScript, we can use square brackets.

Categories
JavaScript Answers

How to removing element from array in component state with React?

Sometimes, we want to removing element from array in component state with React.

In this article, we’ll look at how to removing element from array in component state with React.

How to removing element from array in component state with React?

To removing element from array in component state with React, we use the filter method.

For instance, we write

//...

class Comp extends React.Component {
  //...
  removeItem = (index) => {
    this.setState({
      data: this.state.data.filter((_, i) => i !== index),
    });
  };
  //...
}

to add the removeItem method into the Comp component that sets the data state to an array set as the original value of the data state but without the item at index.

We call filter with (_, i) => i !== index to return an array without the index.

Conclusion

To removing element from array in component state with React, we use the filter method.

Categories
JavaScript Answers

How to access event to call preventdefault from custom function originating from onclick with JavaScript?

Sometimes, we want to access event to call preventdefault from custom function originating from onclick with JavaScript.

In this article, we’ll look at how to access event to call preventdefault from custom function originating from onclick with JavaScript.

How to access event to call preventdefault from custom function originating from onclick with JavaScript?

To access event to call preventdefault from custom function originating from onclick with JavaScript, we get the event object from the event handler function’s parameter.

For instance, we write

<a href="#">Click to say Hi</a>

to add a link.

Then we show an alert when we click on the link by writing

const a = document.querySelector("a");
a.onclick = (e) => {
  e.preventDefault();
  alert("hi");
};

We select the link with querySelector.

Then we set its onclick property to a function that calls e.preventDefault to stop the default behavior of navigation to the URL set as the href attribute’s value.

Instead, we call alert with 'hi' to show an alert box.

Conclusion

To access event to call preventdefault from custom function originating from onclick with JavaScript, we get the event object from the event handler function’s parameter.