JavaScript OOP(一) -- 数据类型
typeof方法
?
返回一个用来表示表达式的数据类型的字符串:"number" "string" "boolean" "object" "function" 和 "undefined"
number?在 Jscript 中整数和浮点值没有差别;JScript 数值可以是其中任意一种(JScript 内部将所有的数值表示为浮点值)。
?
?另外,JScript包含特殊值数字。它们是:
?Number.NaN
?表示算术表达式返回非数字值的特殊值。
?NaN 不与任何值相等,包括其本身。要检测值是否为 NaN,请使用 isNaN 函数。
?
?Number.MIN_VALUE
?返回 JScript 中能够表示的最接近零的数。约等于 2.22E-308。
?
?Number.MAX_VALUE
?返回 JScript 能表达的最大的数。约等于 1.79E+308。
?
?Number.NEGATIVE_INFINITY
?返回比 JScript 能够表示的最小负数(-Number.MAX_VALUE)更小的值。
?JScript 将 NEGATIVE_INFINITY 值显示为 -infinity。该值数学上的作用与负无穷相同。
?
?Number.POSITIVE_INFINITY
?返回比在 JScript 中能够表示的最大的数 (Number.MAX_VALUE) 更大的值。
?JScript 将 POSITIVE_INFINITY 值显示为 infinity。该值数学上的作用与正无穷相同。?(注:与NaN不同,Finity是相等的)
?
?两个相关方法:
?isNaN
?返回一个 Boolean 值,指明提供的值是否是保留值 NaN (不是数字)。
?还有一种办法,变量可以与它自身进行比较。如果比较的结果不等,那么它就是NaN。这是因为NaN是唯一与自身不等的值。
?
?isFinite
?返回一个 Boolean 值,指明所提供的数字是否是有限的。
?如果 number 不是 NaN 、负无穷或正无穷,那么 isFinite 方法将返回 true 。 如果是这三种情况,函数返回 false 。
<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?一个字符串值是排在一起的一串零或零以上的 Unicode 字符(字母、数字和标点符号)。字符串数据类型用来表示 JScript 中的文本。脚本中可以包含字符串文字,这些字符串文字放在一对匹配的的单引号或双引号中。字符串中可以包含双引号,该双引号两边需加单引号,也可以包含单引号,该单引号两边需加双引号。
?
?请注意,JScript 中没有表示单个字符的类型(如 C++ 的 char)。要表示 Jscript 中的单个字符,应创建一个只包含一个字符的字符串。包含零个字符("")的字符串是空(零长度)字符串。function
?
?function A(){} <=> var A = function() {} (唯一区别在预编译阶段)
?
?几个重要的属性(OOP部分不包含在此)
?arguments
?arguments 对象的 length 属性包含了传递给函数的参数的数目。对于arguments 对象所包含的单个参数,其访问方法与数组中所包含的参数的访问方法相同。
?
?arguments.callee
?返回正被执行的Function对象,也就是所指定的Function对象的正文,仅当相关函数正在执行时才可用。
?
?caller
?返回一个对函数的引用,该函数调用了当前函数。
?对于函数来说,caller 属性只有在函数执行时才有定义。如果函数是由JScript程序的顶层调用的,那么caller包含的就是null。
<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? 略
?
? 特殊的object类型null(typeof(null) == object && (null instanceof Object) == false)
? 通过给一个变量赋null值来清除变量的内容。
?
?对象属性与null的比较,以下情况结果为true
?如果属性 someObject.prop 包含 null 值
?如果属性 someObject.prop 不存在(null == undefined, 但是(null === undefined) == false)
?
?要检查一个对象属性是否存在,可以使用新的 in 运算符
?
<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?如下情况使返回 undefined 值:
?对象属性不存在
?声明了变量但从未赋值
?
?测试已声明变量是否为undefined
?x == undefined //right, IE5.5以上版本中,undefined是一个已实现的系统保留字
?typeof(x) == undefined //wrong
?typeof(x) == "undefined" //right
?
?其它产生undefined结果的语句
?void方法返回值
?无return语句的function执行结果
?
<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?true/false
?任何值为0、null、undefined、空字符串的表达式被解释为false,并且除null和undefined之外==false。其他任意值的表达式解释为 true。
?
<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>