Categories
JavaScript Answers

How to make a div visible and invisible with JavaScript?

Sometimes, we want to make a div visible and invisible with JavaScript.

In this article, we’ll look at how to make a div visible and invisible with JavaScript.

How to make a div visible and invisible with JavaScript?

To make a div visible and invisible with JavaScript, we set the display or visibility properties.

For instance, we write

elem.style.display = 'block';
elem.style.display = 'inline';

elem.style.visibility = 'visible';

to set the display property to 'block' or 'inline' to make the elem element display as a block or inline element.

Or we set visbility to 'visible' to show the element.

To hide the element, we write

elem.style.display = 'none'; 

elem.style.visibility = 'hidden';

to hide the elem element by setting display to 'none' without keeping the space for the element.

Or we hide the element and keep the space for the element by setting visibility to 'hidden‘.

Conclusion

To make a div visible and invisible with JavaScript, we set the display or visibility properties.

Categories
JavaScript Answers

How to create an abstract base class in JavaScript?

Sometimes, we want to create an abstract base class in JavaScript.

In this article, we’ll look at how to create an abstract base class in JavaScript.

How to create an abstract base class in JavaScript?

To create an abstract base class in JavaScript, we can create a class that other classes can inherit from.

For instance, we write

class Animal {
  constructor() {
    if (this.constructor === Animal) {
      throw new Error("Abstract classes can't be instantiated.");
    }
  }

  say() {
    throw new Error("Method 'say()' must be implemented.");
  }

  eat() {
    console.log("eating");
  }
}

class Dog extends Animal {
  say() {
    console.log("bark");
  }
}

to create the Animal class which throws errors for methods that should be implemented by child classes.

In Animal‘s constructor, we throw an error if the current class is Animal so that we can’t instantiate Animal directly.

Then we create the Dog class, which is a child class of Animal, that implements the methods by overriding the ones in Animal.

Conclusion

To create an abstract base class in JavaScript, we can create a class that other classes can inherit from.

Categories
JavaScript Answers

How to create array with function similar to Python range() with JavaScript?

Sometimes, we want to create array with function similar to Python range() with JavaScript.

In this article, we’ll look at how to create array with function similar to Python range() with JavaScript.

How to create array with function similar to Python range() with JavaScript?

To create array with function similar to Python range() with JavaScript, we can use the Array constructor and the spread operator.

For instance, we write

const range = (n) => [...Array(n).keys()];

to create an array with length n with Array(n).

Then we call keys to return an array of indexes of the array.

And then we spread the indexes of the array into a new array.

Conclusion

To create array with function similar to Python range() with JavaScript, we can use the Array constructor and the spread operator.

Categories
React Answers

How to fix setTimeout() not working with React?

Sometimes, we want to fix setTimeout() not working with React.

In this article, we’ll look at how to fix setTimeout() not working with React.

How to fix setTimeout() not working with React?

To fix setTimeout() not working with React, we can use call setState in the setTimneout callback.

For instance, we write

setTimeout(() => this.setState({ position: 1 }), 3000);

to call setTimeout with a callback that calls this.setState.

We use an arrow function for the callback since it doesn’t bind to its own value of this.

This means this will still be the component instance.

Conclusion

To fix setTimeout() not working with React, we can use call setState in the setTimneout callback.

Categories
JavaScript Answers

How to add or update an attribute to an HTML element using JavaScript?

Sometimes, we want to add or update an attribute to an HTML element using JavaScript.

In this article, we’ll look at how to add or update an attribute to an HTML element using JavaScript.

How to add or update an attribute to an HTML element using JavaScript?

To add or update an attribute to an HTML element using JavaScript, we can set the property in the attributes property.

For instance, we write

const e = document.createElement("div");
e.attributes["id"] = "div1";

to create a div with createElement.

Then we set the id attribute of the div with

e.attributes["id"] = "div1";

Conclusion

To add or update an attribute to an HTML element using JavaScript, we can set the property in the attributes property.