1

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..

var StringWrapper = function(string) {
    this.string = string;
};

StringWrapper.prototype.contains = function (string) {
    if (this.string.indexOf(string) >= 0)
        return true;
    return false;
};

var myString = new StringWrapper("hey there");

if(myString.contains("hey"))
   alert(myString); // should alert "hey there"

if(myString == "hey there") // should be true
   doSomething();

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.

16
  • means do you want to get that value in console.log ?? Commented Mar 31, 2015 at 14:30
  • 2
    Do you really have to use the new MyClass() construct? It sounds like a "normal" function would work better. Commented Mar 31, 2015 at 14:30
  • 2
    We get that it's just an example, but based on that example you'd be better off using just a simple function to return the value, or if that doesn't work for you then the example doesn't really represent your actual problem so it's very hard to say what you actually need. Commented Mar 31, 2015 at 14:38
  • 2
    @Andy What is a little example? You haven't given us any example of what you want to do. Commented Mar 31, 2015 at 14:39
  • 1
    You're again stopping short of showing the most important part... an example that shows the circumstance in which you actually need to get the string value... or is it truly just for logging to the console? Commented Mar 31, 2015 at 14:55

1 Answer 1

8

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

var MyClass = function(value) {
  this.value = value;
};

MyClass.prototype.toString = function() {
  return this.value;
};


var classObj = new MyClass("hey there");

snippet.log(classObj);
snippet.log(classObj + "!");
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

With ES6 class syntax:

class MyClass {
    constructor(value) {
        this.value = value;
    }

    toString() {
        return this.value;
    }
}

var classObj = new MyClass("hey there");

console.log(classObj);
console.log(classObj + "!");   
Sign up to request clarification or add additional context in comments.

8 Comments

Although this completely answers the question, it is still makes no sense whatsoever lol. Creating an object to wrap an string, to use the toString interface to get the string back.
@ikaros45 It doesn't make sense in this very simplistic case, but it could make sense if the object contained other stuff and that might be what OP is trying to get at.
You may be right sir, if what he intends is something like an repr in python, ie a representation of the object, that is ok... but from the question I would not say so.
Implementing the custom toString method is perfect, that's all I was looking to do - I just didn't quite know that's what I was looking to do. :D
Most of the time, most of the people mostly don't know what they are looking (includes me of course) :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.