var now=new Date();
alert(typeof(now + 1)); // 调用now.toString(),然后与1拼接,这时就成字符串了,所以alert是string
这里有个示例:
var now=new Date();
// 改写now对象的toString(),返回一个number类型的
now.toString=function(){
return 2;
}
alert(typeof(now + 1)); // number类型相加,也是number类型的,所以alert是number
很简单,因为now是一个object对象,而当object跟一个number相加,会想先调用now.toString()方法,得到的最终结果就是一个字符串。
Date是对象,默认会隐式转换为字符串,即调用toString(), 不会转换为数字直接量的
要显式转换为数字: now.getTime()
typeof 函数返回的值就是字符串类型的 。取出参数的类型,无论里面传递的是什么。