var line = 0;
function log(str){
line++;
document.write("<p><span style='color:blue;'>"+line+"</span>\toutput:<strong><b style='color:red;'>"
+str+"</b></strong> [Time:"+new Date()+"]</p>")
}
//log("OK");
//OOP
//1
function Obj(){
var obj = new Object();
obj.name="Obj";
return obj;//必须返回,否则,输出的时候name为undefined!
}
log(typeof new Obj());
log(new Obj().name);
//2
function Clazz(){
this.name="Clazz";
}
log(typeof new Clazz());
log(new Clazz().name);
//json obj
//1
var directJsonObj = {name:"JSON Object"};
log(typeof directJsonObj);
log(directJsonObj);
log(JSON.stringify(directJsonObj));
//2
var jsonStr = '{name:"JSON Object"}';
var jsonObj = eval("("+jsonStr+")");
log(typeof jsonObj);
log(jsonObj);
log(JSON.stringify(jsonObj));
log(jsonObj.toString());
log(jsonObj.toJSONString);//这个方法undefined!
log("============");
if(jsonStr.parseJSON){//这个方法undefined!
log(typeof jsonObj);
log(jsonObj);
log(JSON.stringify(jsonObj));
log(jsonObj.toString());
log(jsonObj.toJSONString);
}
1 output:object [Time:Sat May 19 2018 13:04:08 GMT+0800 (中国标准时间)]
2 output:Obj [Time:Sat May 19 2018 13:04:08 GMT+0800 (中国标准时间)]
3 output:object [Time:Sat May 19 2018 13:04:08 GMT+0800 (中国标准时间)]
4 output:Clazz [Time:Sat May 19 2018 13:04:08 GMT+0800 (中国标准时间)]
5 output:object [Time:Sat May 19 2018 13:04:08 GMT+0800 (中国标准时间)]
6 output:[object Object] [Time:Sat May 19 2018 13:04:08 GMT+0800 (中国标准时间)]
7 output:{"name":"JSON Object"} [Time:Sat May 19 2018 13:04:08 GMT+0800 (中国标准时间)]
8 output:object [Time:Sat May 19 2018 13:04:08 GMT+0800 (中国标准时间)]
9 output:[object Object] [Time:Sat May 19 2018 13:04:08 GMT+0800 (中国标准时间)]
10 output:{"name":"JSON Object"} [Time:Sat May 19 2018 13:04:08 GMT+0800 (中国标准时间)]
11 output:[object Object] [Time:Sat May 19 2018 13:04:08 GMT+0800 (中国标准时间)]
12 output:undefined [Time:Sat May 19 2018 13:04:08 GMT+0800 (中国标准时间)]
13 output:============ [Time:Sat May 19 2018 13:04:08 GMT+0800 (中国标准时间)]