javascript 为什么 location.toString() 报告与 location.href 相同?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6329092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does location.toString() report the same as location.href?
提问by Dean James
The window.location is an object. But when you execute location.toString()
it converts the object to the equivalent to location.href
.
window.location 是一个对象。但是当您执行location.toString()
它时,它会将对象转换为location.href
.
My question is how? And can I set up objects to a similar behaviour?
我的问题是如何?我可以将对象设置为类似的行为吗?
回答by Mic
You can add a toString
method to your object that returns what you want. In that case href
您可以toString
向对象添加一个返回所需内容的方法。在这种情况下href
eg:
例如:
var obj = {
href:'',
toString:function(){
return this.href;
}
};
obj.href = 'http://stackoverflow.com';
obj.toString();