JavaScript OOP(一) -- 数据类型
typeof方法
?
返回一个用来表示表达式的数据类型的字符串:"number" "string" "boolean" "object" "function" 和 "undefined"
number<html><head><title>number test</title></head><body><input type="button" value="typeof(0)" onclick="javascript:alert(typeof(0));"/><br><input type="button" value="number.MIN_VALUE" onclick="javascript:alert(Number.MIN_VALUE);"/><br><input type="button" value="number.MAX_VALUE" onclick="javascript:alert(Number.MAX_VALUE);"/><br><input type="button" value="number.NEGATIVE_INFINITY" onclick="javascript:alert(Number.NEGATIVE_INFINITY);"/><br><input type="button" value="number.POSITIVE_INFINITY" onclick="javascript:alert(Number.POSITIVE_INFINITY);"/><br><input type="button" value="isNaN(0)" onclick="javascript:alert(isNaN(0));"/><br><input type="button" value="isNaN('a')" onclick="javascript:alert(isNaN('a'));"/><br><input type="button" value="isFinite(Number.NaN)" onclick="javascript:alert(isFinite(Number.NaN));"/><br><input type="button" value="isFinite(Number.POSITIVE_INFINITY)" onclick="javascript:alert(isFinite(Number.POSITIVE_INFINITY));"/><br><input type="button" value="isFinite(Number.NEGATIVE_INFINITY)" onclick="javascript:alert(isFinite(Number.NEGATIVE_INFINITY));"/><br><input type="button" value="Number.POSITIVE_INFINITY" onclick="javascript:alert(Number.POSITIVE_INFINITY*2 == Number.POSITIVE_INFINITY);"/><br></body></html>string<html><head><title>function test</title></head><body><script language="javascript"><!--function foo1(arg) {foo2(arg, 'b');}function foo2(arg1, arg2) {foo3(arg1, arg2, 'c');}function foo3() {var foo = arguments.callee;while (foo) {var args = foo.arguments;for (var i = 0; i < args.length; i++) {document.write(args[i]);}foo = foo.caller;document.write("<br/>");}}foo1('a');--></script></body></html>object<html><head><title>null test</title><script language="javascript"><!--var nullVar = null;--></script></head><body><input type="button" value="typeof(null)" onclick="javascript:alert(typeof(null));"/><br><input type="button" value="nullVar == null" onclick="javascript:alert(nullVar == null);"/><br><input type="button" value="window.unexistedProp == null" onclick="javascript:alert(window.unexistedProp == null);"/><br><input type="button" value="'unexistedProp' in window" onclick="javascript:alert('unexistedProp' in window);"/><br><input type="button" value="null instanceof Object" onclick="javascript:alert(null instanceof Object);"/><br><input type="button" value="null == undefined" onclick="javascript:alert(null == undefined);"/><br><input type="button" value="null === undefined" onclick="javascript:alert(null === undefined);"/><br></body></html>?undefined<html><head><title>undefined test</title><script language="javascript"><!--var undefined2;--></script></head><body><input type="button" value="alert(window.undefined1)" onclick="javascript:alert(window.undefined1);"/><br><input type="button" value="alert(unassigned undefined2)" onclick="javascript:alert(undefined2);"/><br><input type="button" value="alert(undefined2 == undefined)" onclick="javascript:alert(undefined2 == undefined);"/><br><input type="button" value="alert(typeof(undefined2) == undefined)" onclick="javascript:alert(typeof(undefined2) == undefined);"/><br><input type="button" value="alert(typeof(undefined2) == 'undefined')" onclick="javascript:alert(typeof(undefined2) == 'undefined');"/><br><input type="button" value="alert(void(0))" onclick="javascript:alert(void(0));"/><br><input type="button" value="alert(function(){}())" onclick="javascript:alert(function(){}());"/><br></body></html>boolean<html><head><title>boolean test</title></head><body><input type="button" value="alert(0 == true)" onclick="javascript:alert(0 == true);"/><br><input type="button" value="alert(null == true)" onclick="javascript:alert(null == true);"/><br><input type="button" value="alert(undefined == true)" onclick="javascript:alert(undefined == true);"/><br><input type="button" value="alert('' == true)" onclick="javascript:alert('' == true);"/><br><input type="button" value='alert("" == true)' onclick='javascript:alert("" == true);'/><br><input type="button" value="alert(0 == false)" onclick="javascript:alert(0 == false);"/><br><input type="button" value="alert(null == false)" onclick="javascript:alert(null == false);"/><br><input type="button" value="alert(undefined == false)" onclick="javascript:alert(undefined == false);"/><br><input type="button" value="alert('' == false)" onclick="javascript:alert('' == false);"/><br><input type="button" value='alert("" == false)' onclick='javascript:alert("" == false);'/><br></body></html>