5
window.location是一个对象。但是当您执行location.toString()
时,它会将对象转换为相当于location.href
的对象。为什么location.toString()报告与location.href相同?
我的问题是怎么回事?我可以将对象设置为类似的行为吗?
window.location是一个对象。但是当您执行location.toString()
时,它会将对象转换为相当于location.href
的对象。为什么location.toString()报告与location.href相同?
我的问题是怎么回事?我可以将对象设置为类似的行为吗?
您可以将toString
方法添加到返回所需内容的对象。在这种情况下href
例如:
var obj = {
href:'',
toString:function(){
return this.href;
}
};
obj.href = 'http://stackoverflow.com';
obj.toString();
所有对象都有一个toString()方法。您可以通过更改原型实现或具体实例的实现来覆盖它。你应该更精确地知道你想做什么。 – jjrdk 2011-06-13 10:14:32
补充@ jjrdk的评论:对于'window.location','toString()'方法以这样的方式实现,即它返回'href'属性。 – Tomalak 2011-06-13 10:20:29