Categories
JavaScript Answers

How to add breakpoint on property change with JavaScript?

Sometimes, we want to add breakpoint on property change with JavaScript.

In this article, we’ll look at how to add breakpoint on property change with JavaScript.

How to add breakpoint on property change with JavaScript?

To add breakpoint on property change with JavaScript, we can define the property with a getter and setter.

Then we add debugger in the setter`.

For instance, we write

const obj = {
  someProp: 10,
};

obj._someProp = obj.someProp;

Object.defineProperty(obj, "someProp", {
  get() {
    return obj._someProp;
  },

  set(value) {
    debugger;
    obj._someProp = value;
  },
});

to call Object.defineProperty to add the someProp property to obj.

We add the getter with get and the setter with set.

We assigned the original someProp property to _someProp and we override the someProp property with the new one we just defined.

debugger is added to set to add a breakpoint when the value changes.

Conclusion

To add breakpoint on property change with JavaScript, we can define the property with a getter and setter.

Then we add debugger in the setter`.

Categories
JavaScript Answers

How to open URL in new window with JavaScript?

Sometimes, we want to open URL in new window with JavaScript.

In this article, we’ll look at how to open URL in new window with JavaScript.

How to open URL in new window with JavaScript?

To open URL in new window with JavaScript, we call window.open with '_blank'.

For instance, we write

window.open(
  url,
  "_blank",
  "location=yes,height=570,width=520,scrollbars=yes,status=yes"
);

to call window.open to open the url.

And we set the 2nd argument to '_blank' to make it open in a new window.

We then set the window’s height, width, scrollbars, and status bar options with the string in the 3rd argument.

Conclusion

To open URL in new window with JavaScript, we call window.open with '_blank'.

Categories
JavaScript Answers

How to get and set the current web page scroll position with JavaScript?

Sometimes, we want to get and set the current web page scroll position with JavaScript.

In this article, we’ll look at how to get and set the current web page scroll position with JavaScript.

How to get and set the current web page scroll position with JavaScript?

To get and set the current web page scroll position with JavaScript, we can use the scrollTop property.

For instance, we write

document.documentElement.scrollTop || document.body.scrollTop

to get the scroll position with either property.

And we can set the scroll position with

document.documentElement.scrollTop = document.body.scrollTop = 1000;

where 1000 is in pixels to scroll the page 1000 pixels down.

Conclusion

To get and set the current web page scroll position with JavaScript, we can use the scrollTop property.

Categories
JavaScript Answers

How to convert object string to JSON with JavaScript?

Sometimes, we want to convert object string to JSON with JavaScript.

In this article, we’ll look at how to convert object string to JSON with JavaScript.

How to convert object string to JSON with JavaScript?

To convert object string to JSON with JavaScript, we can use eval to convert the object string to valid JSON if the string is trusted for sure.

For instance, we write

const str =
  "{ hello: 'world', places: ['Africa', 'America', 'Asia', 'Australia'] }";
const json = JSON.stringify(eval("(" + str + ")"));

to call eval on the str string to convert it to a valid JavaScript object.

Then we call JSON.stringify on the returned object to convert it to a valid JSON string.

Conclusion

To convert object string to JSON with JavaScript, we can use eval to convert the object string to valid JSON if the string is trusted for sure.

Categories
JavaScript Answers

How to fix useState not triggering re-render with React?

Sometimes, we want to fix useState not triggering re-render with React.

In this article, we’ll look at how to fix useState not triggering re-render with React.

How to fix useState not triggering re-render with React?

To fix useState not triggering re-render with React, we should make a copy of objects or arrays before we update them.

For instance, we write

import React, { useState, useEffect } from "react";

const Comp = () => {
  //...
  const [state, setState] = {};

  const f = () => {
    setState({ ...state });
  };
  //...
};

to call setState with a copy of state in the function f to trigger re-rendering of the Comp component.

Conclusion

To fix useState not triggering re-render with React, we should make a copy of objects or arrays before we update them.