Skip to content
Advertisement

Return JavaScript class value instead of object reference

I would like to know if there is a way to return a JS class’s value by default instead of of reference to the class object itself. Let’s say, for example, I want to wrap a string..

JavaScript

and now I want to get string just by using myString rather than myString.string. Is this doable somehow?

Edit

I took the console.log(myString) out of the question, because console.log has behavior that I didn’t originally take into account. This question isn’t about log.

Advertisement

Answer

Your question doesn’t entirely make sense, but it kind of sounds like you want to implement the .toString interface:

JavaScript
JavaScript

With ES6 class syntax:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement