admin管理员组文章数量:1287933
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?
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?
Share Improve this question edited Feb 19, 2015 at 4:34 Timo Tijhof 10.3k6 gold badges37 silver badges53 bronze badges asked Jun 13, 2011 at 10:11 Dean JamesDean James 2,6334 gold badges23 silver badges30 bronze badges 3- 1 All objects have a toString() method. You can override it either by changing the prototype implementation, or the implementation for the concrete instance. You should be more precise as to what you want to do. – jjrdk Commented Jun 13, 2011 at 10:14
-
To plement @jjrdk's ment: For
window.location
, thetoString()
method is implemented in such a way that it returns thehref
property. – Tomalak Commented Jun 13, 2011 at 10:20 - 1 To plement @Tomalak answer, here is the proof developer.mozilla/en-US/docs/Web/API/Location/toString – woto Commented Jun 15, 2022 at 17:41
1 Answer
Reset to default 8You 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.';
obj.toString();
本文标签: javascriptWhy does locationtoString() report the same as locationhrefStack Overflow
版权声明:本文标题:javascript - Why does location.toString() report the same as location.href? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741278180a2369853.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论