javascript在不同浏览器中的类型测试实验(跨浏览器编程要注意了!!)

为了测试javascript中typeof和Object.prototype.toString的各个native object在不同的浏览器的运行结果,特写了以下的测试代码:

var def;
var gPrimitiveType = {
'string':'str',
'boolean':true,
'function':function(){},
'object':{},
'array':[],
'undefined':def
};

var ul = $('content');
QQVIP.object.each(gPrimitiveType, function(value,key){

var child = document.createElement('LI');
var text = 'typeof ' + key + ' is:' + (typeof value);
child.appendChild(document.createTextNode(text));
ul.appendChild(child);

});

var gProtoType = {
'String.prototype':String.prototype,
'Function.prototype':Function.prototype,
'Object.prototype':Object.prototype,
'Array.prototype':Array.prototype,
'Date.prototype':Date.prototype,
'RegExp.prototype':RegExp.prototype
};

var ul = $('typelist');
QQVIP.object.each(gProtoType, function(value,key){

var child = document.createElement('LI');
var text = 'Object.prototype.toString.call( ' + key + ') is:' + (Object.prototype.toString.call(value) );
child.appendChild(document.createTextNode(text));
ul.appendChild(child);

});

下面是在各个浏览器下的运行结果:

1,IE6下:

typeof 运算结果:

  • typeof string is:string
  • typeof boolean is:boolean
  • typeof function is:function
  • typeof object is:object
  • typeof array is:object
  • typeof undefined is:undefined

Object.prototype.toString.call() 运算结果:

  • Object.prototype.toString.call( String.prototype) is:[object Object]
  • Object.prototype.toString.call( Function.prototype) is:[object Function]
  • Object.prototype.toString.call( Object.prototype) is:[object Object]
  • Object.prototype.toString.call( Array.prototype) is:[object Array]
  • Object.prototype.toString.call( Date.prototype) is:[object Date]
  • Object.prototype.toString.call( RegExp.prototype) is:[object RegExp]

二,Firefox3.0.15

typeof 运算结果:

  • typeof string is:string
  • typeof boolean is:boolean
  • typeof function is:function
  • typeof object is:object
  • typeof array is:object
  • typeof undefined is:undefined

Object.prototype.toString.call() 运算结果:

  • Object.prototype.toString.call( String.prototype) is:[object String]
  • Object.prototype.toString.call( Function.prototype) is:[object Function]
  • Object.prototype.toString.call( Object.prototype) is:[object Object]
  • Object.prototype.toString.call( Array.prototype) is:[object Array]
  • Object.prototype.toString.call( Date.prototype) is:[object Date]
  • Object.prototype.toString.call( RegExp.prototype) is:[object RegExp]

三,Chrome4.0

typeof 运算结果:

  • typeof string is:string
  • typeof boolean is:boolean
  • typeof function is:function
  • typeof object is:object
  • typeof array is:object
  • typeof undefined is:undefined

Object.prototype.toString.call() 运算结果:

  • Object.prototype.toString.call( String.prototype) is:[object String]
  • Object.prototype.toString.call( Function.prototype) is:[object Function]
  • Object.prototype.toString.call( Object.prototype) is:[object Object]
  • Object.prototype.toString.call( Array.prototype) is:[object Array]
  • Object.prototype.toString.call( Date.prototype) is:[object Date]
  • Object.prototype.toString.call( RegExp.prototype) is:[object Object]

四:Opera9.20

typeof 运算结果:
typeof string is:string
typeof boolean is:boolean
typeof function is:function
typeof object is:object
typeof array is:object
typeof undefined is:undefined
Object.prototype.toString.call() 运算结果:
Object.prototype.toString.call( String.prototype) is:[object String]
Object.prototype.toString.call( Function.prototype) is:[object Function]
Object.prototype.toString.call( Object.prototype) is:[object Object]
Object.prototype.toString.call( Array.prototype) is:[object Array]
Object.prototype.toString.call( Date.prototype) is:[object Date]
Object.prototype.toString.call( RegExp.prototype) is:[object RegExp]

五,Safari3.1.2

typeof 运算结果:

  • typeof string is:string
  • typeof boolean is:boolean
  • typeof function is:function
  • typeof object is:object
  • typeof array is:object
  • typeof undefined is:undefined

Object.prototype.toString.call() 运算结果:

  • Object.prototype.toString.call( String.prototype) is:[object String]
  • Object.prototype.toString.call( Function.prototype) is:[object Function]
  • Object.prototype.toString.call( Object.prototype) is:[object Object]
  • Object.prototype.toString.call( Array.prototype) is:[object Array]
  • Object.prototype.toString.call( Date.prototype) is:[object Date]
  • Object.prototype.toString.call( RegExp.prototype) is:[object RegExpPrototype]

综上,标红部分都是跟其他不一样的,需要充分注意。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值