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
.
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
.
-
means do you want to get that value in
console.log
?? – Mritunjay 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. – JJJ 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. – JJJ 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. – JLRishe 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? – user1106925 Commented Mar 31, 2015 at 14:55
1 Answer
Reset to default 8Your 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./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 + "!");
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744342003a4569454.html
评论列表(0条)