Dean James
Dean James

Reputation: 2623

Why does location.toString() report the same as location.href?

The window.location is an object. But when you execute location.toString() it converts the object to the equivalent to location.href.

My question is how? And can I set up objects to a similar behaviour?

Upvotes: 7

Views: 13140

Answers (1)

Mic
Mic

Reputation: 25164

You can add a toString method to your object that returns what you want. In that case href

eg:

var obj = {
  href:'',
  toString:function(){
    return this.href;
  }
};

obj.href = 'http://stackoverflow.com';
obj.toString();

Upvotes: 8

Related Questions