Categories
JavaScript Answers

How to get the class name of ES6 class instance with JavaScript?

Sometimes, we want to get the class name of ES6 class instance with JavaScript.

In this article, we’ll look at how to get the class name of ES6 class instance with JavaScript.

How to get the class name of ES6 class instance with JavaScript?

To get the class name of ES6 class instance with JavaScript, we can use the name property of the class or the constructor.name property of the class instance.

For instance, we write

class SomeClass {
  constructor() {}
}

const s = new SomeClass();
console.log(s.constructor.name);
console.log(SomeClass.name);

to create the SomeClass class.

We get the class name of the SomeClass instance s with s.constructor.name.

And we get the name of the class from SomeClass directly with SomeClass.name.

They’ll both log 'SomeClass'.

Conclusion

To get the class name of ES6 class instance with JavaScript, we can use the name property of the class or the constructor.name property of the class instance.

Categories
JavaScript Answers

How to set URL query params in Vue with Vue-Router?

Sometimes, we want to set URL query params in Vue with Vue-Router.

in this article, we’ll look at how to set URL query params in Vue with Vue-Router.

How to set URL query params in Vue with Vue-Router?

To set URL query params in Vue with Vue-Router, we call router.push with an object that has the query property.

For instance, we write

router.push({ path: "register", query: { plan: "private" } });

to call router.push with an object that has the query property to add a query parameter with key plan and value private to the end of the URL with path register.

Conclusion

To set URL query params in Vue with Vue-Router, we call router.push with an object that has the query property.

Categories
JavaScript Answers

How to copy JavaScript object to new variable not by reference?

Sometimes, we want to copy JavaScript object to new variable not by reference.

In this article, we’ll look at how to copy JavaScript object to new variable not by reference.

How to copy JavaScript object to new variable not by reference?

To copy JavaScript object to new variable not by reference, we make a copy of the object before we pass it into a function as an argument.

For instance, we write

const copyObj = JSON.parse(JSON.stringify(jsonObj));

to call JSON.stringify with jsonObj and then call JSON.parse on the JSON string to make a deep copy of the object.

And then we assign the returned copied object to copyObj.

Then we can use copyObj as the argument of a function and it doesn’t reference the original jsonObj object.

Conclusion

To copy JavaScript object to new variable not by reference, we make a copy of the object before we pass it into a function as an argument.

Categories
JavaScript Answers

How to convert a binary Node.js Buffer to JavaScript ArrayBuffer?

Sometimes, we want to convert a binary Node.js Buffer to JavaScript ArrayBuffer.

In this article, we’ll look at how to convert a binary Node.js Buffer to JavaScript ArrayBuffer.

How to convert a binary Node.js Buffer to JavaScript ArrayBuffer?

To convert a binary Node.js Buffer to JavaScript ArrayBuffer, we can use the Uint8Array constructor.

For instance, we write

const arrayBuffer = new Uint8Array(nodeBuffer).buffer;

to create the Uint8Array from the nodeBuffer Node.js buffer object.

Then we get the array buffer from the buffer property.

Conclusion

To convert a binary Node.js Buffer to JavaScript ArrayBuffer, we can use the Uint8Array constructor.

Categories
JavaScript Answers

How to split string and keep the separator with JavaScript and regex?

Sometimes, we want to split string and keep the separator with JavaScript and regex.

In this article, we’ll look at how to split string and keep the separator with JavaScript and regex.

How to split string and keep the separator with JavaScript and regex?

To split string and keep the separator with JavaScript and regex, we can call string’s split method with a regex.

For instance, we write

const a = "1、2、3".split(/(、)/g) 

to call split with /(、)/g to return an iterable object with all the values around the delimier in addition to the delimiters themselves.

Therefore, we get returned.

["1", "、", "2", "、", "3"]

Conclusion

To split string and keep the separator with JavaScript and regex, we can call string’s split method with a regex.