Why does location.toString() report the same as location.href?
The window.location is an object. But when you execute location.toString开发者_C百科()
it converts the object to the equivalent to location.href
.
My question is how? And can I set up objects to a similar behaviour?
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();
精彩评论