Why is location.toString () reporting the same as location.href?

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

My question is: how? Can I customize objects for similar behavior?

+5
source share
1 answer

You can add a method toStringto your object that returns what you want. In this casehref

eg:

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

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

All Articles