window.location 是一个对象。但是,当您执行 location.toString()
时,它会将对象转换为等同于 location.href
的对象。
我的问题是如何?我可以将对象设置为类似的行为吗?
最佳答案
您可以将 toString
方法添加到您的对象以返回您想要的内容。在那种情况下 href
例如:
var obj = {
href:'',
toString:function(){
return this.href;
}
};
obj.href = 'http://stackoverflow.com';
obj.toString();
关于javascript - 为什么 location.toString() 报告与 location.href 相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6329092/