Joeytje50
Joeytje50

Reputation: 19112

create a "default" value for an object

Now before I ask my question, let me just say that I know strings are primitive on js and I know you can't add properties to them. Just to get that out of the way before anyone brings it up.

My question is if it would be possible to make an object have a kind of default value, in the same way a date object has it. With date objects if you do typeof date it will return "object", but if you put it inside a string, it will automatically change to something that would look like "Sun Jan 01 2000 01:23:45 GMT+0000 (ABC)".

Is it possible to create an object that would behave in the same way? Or is that behaviour exclusive to date objects?

One example of where I could use it is when running a function, I want to return a string by default, but also send extra data along with it. For example:

function getUser(index) {
    return {
        defaultVal: userNames[index],
        extraData: {points:userPoints[index]}
    }
}
alert("your name is "+getUser(userId))
//will alert the user's name, instead of json object

Upvotes: 0

Views: 105

Answers (3)

thefourtheye
thefourtheye

Reputation: 239473

As per alert's docs,

message is an optional string of text you want to display in the alert dialog, or, alternatively, an object that is converted into a string and displayed.

So, you need to override the default toString method, like this, to give represent your object in the alert.

function myString (value) {
    this.actualValue = value;
}

myString.prototype.toString = function() {
    console.log("Inside myString.toString, returning", this.actualValue);
    return this.actualValue;
};

function getUser() {
    var myUser = new myString("Bruce Wayne");
    myUser.extraData = "I am BatMan!";
    return myUser;
}

alert(getUser());

Demo

As per the toString docs,

Every object has a toString() method that is automatically called when the object is to be represented as a text value or when an object is referred to in a manner in which a string is expected. By default, the toString() method is inherited by every object descended from Object. If this method is not overridden in a custom object, toString() returns "[object type]", where type is the object type.

So, this new function, which we defined, will be called even in the expressions where you use the object like a string. For example,

console.log("The value of the Object is " + getUser());

Upvotes: 1

scrblnrd3
scrblnrd3

Reputation: 7416

Well you can actually add properties to strings, like this

String.prototype.foo=function(bar){
    return this+bar;
}

var str="hello, ";
alert(str.foo("world!"));

So I think what you might want be something like this

myObj={foo:"bar","hello":world,toString:function(){
    return this.foo+", "+this.world;
}};

and then instead of printing your myObj, you can print out myObj.toString()

Or in your case, something like this

getUser.prototype.toString=function(){
    return this.defaultVal+this.extraData.userPoints[index]
}

Upvotes: 0

Rob
Rob

Reputation: 12872

I believe you want to override the toString() method.

getUser.prototype.toString() = function() {
    return this.stringRepresentation;
};

Upvotes: 2

Related Questions