JavaScript OOP 1

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 (中国标准时间)]



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值