javascript在不同浏览器中的类型测试实验(跨浏览器编程要注意了!!) ...
时间:2010-08-09 来源:tenfyguo
为了测试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]
综上,标红部分都是跟其他不一样的,需要充分注意。