Categories
JavaScript Answers

How to clone a JavaScript class instance?

Sometimes, we want to clone a JavaScript class instance?.

In this article, we’ll look at how to clone a JavaScript class instance.

How to clone a JavaScript class instance?

To clone a JavaScript class instance, we can use some object methods.

For instance, we write:

class Foo {
  constructor(x) {
    this.x = x
  }
}

const foo = new Foo('bar')
const fooClone = Object.assign(Object.create(Object.getPrototypeOf(foo)), foo)
console.log(foo)
console.log(fooClone)

to create the Foo class.

Then we create a new Foo instance and assign it to foo.

To create a clone of foo, we call Object.create to create a new Foo instance with Object.getPrototypeOf(foo).

Then we call Object.assign to merge the contents of foo into the cloned object.

Conclusion

To clone a JavaScript class instance, we can use some object methods.

Categories
JavaScript Answers

How to set an img element’s src attribute with a JavaScript function’s return value?

To set an img element’s src attribute with a JavaScript function’s return value, we can select the img element and set its src property.

For instance, we write:

<img />

to add an img element.

Then we write:

const getImgSrc = () => 'https://picsum.photos/200/300'

const img = document.querySelector('img')
img.src = getImgSrc()

to define the getImgSrc function to return the image URL string.

Then we select the img element with querySelector.

And then we set img.src to the value returned by getImgSrc.

Categories
JavaScript Answers

How to make fetch API work with CORS after OPTIONS response with JavaScript?

Sometimes, we want to make fetch API work with CORS after OPTIONS response with JavaScript.

In this article, we’ll look at how to make fetch API work with CORS after OPTIONS response with JavaScript.

How to make fetch API work with CORS after OPTIONS response with JavaScript?

To make fetch API work with CORS after OPTIONS response with JavaScript, we can just call fetch.

For instance, we write:

(async () => {
  const response = await fetch("https://jsonplaceholder.typicode.com/posts")
  const data = await response.json()
  console.log(data)
})()

We make a GET request with fetch.

Then we get the JSON response body with response.json.

Conclusion

To make fetch API work with CORS after OPTIONS response with JavaScript, we can just call fetch.

Categories
JavaScript Answers

How to determine if an object property exists and is not empty with JavaScript?

Sometimes, we want to determine if an object property exists and is not empty with JavaScript.

In this article, we’ll look at how to determine if an object property exists and is not empty with JavaScript.

How to determine if an object property exists and is not empty with JavaScript?

To determine if an object property exists and is not empty with JavaScript, we can use the hasOwnProperty method.

For instance, we write:

const errors = {
  error1: "Error 1 description",
  error2: "Error 2 description",
  error3: "",
  error4: "Error 4 description"
};

if (errors.hasOwnProperty('error2') && errors.error2 !== undefined) {
  // ...
}

We call errors.hasOwnProperty with 'error2' to check if the error2 property is in errors.

And then we check if errors.error2isn't set toundefinedwitherrors.error2 !== undefined`.

Conclusion

To determine if an object property exists and is not empty with JavaScript, we can use the hasOwnProperty method.

Categories
JavaScript Answers

How to get the last item of array without pop with JavaScript?

Sometimes, we want to get the last item of array without pop with JavaScript.

In this article, we’ll look at how to get the last item of array without pop with JavaScript.

How to get the last item of array without pop with JavaScript?

To get the last item of array without pop with JavaScript, we can use the array at method.

For instance, we write:

const array = [1, 2, 3, 5, 7];
const last = array.at(-1)
console.log(last)

We call array.at with -1 to return the last entry of array.

Therefore, last is 7.

Conclusion

To get the last item of array without pop with JavaScript, we can use the array at method.