( ! ) Warning: include(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in D:\www\up\js\datatype.php on line 14
Call Stack
#TimeMemoryFunctionLocation
10.0000368024{main}( )...\datatype.php:0

( ! ) Warning: include(http://pub.houheaven.com/Nav02/Nav_deep2.htm): failed to open stream: no suitable wrapper could be found in D:\www\up\js\datatype.php on line 14
Call Stack
#TimeMemoryFunctionLocation
10.0000368024{main}( )...\datatype.php:0

( ! ) Warning: include(): Failed opening 'http://pub.houheaven.com/Nav02/Nav_deep2.htm' for inclusion (include_path='.;C:\php\pear') in D:\www\up\js\datatype.php on line 14
Call Stack
#TimeMemoryFunctionLocation
10.0000368024{main}( )...\datatype.php:0

信息:原创2014-02-050 次阅读0 个评论

标签:后天堂向上,网站制作,自学教程,网站技术,javascript,javascript数据类型


Javascript 含有六种原始类型,分别是 Object、Number、String、Boolean、Undefined、Null。

Object

Object 类自身用处不大,因为 Javascript 中其他所有的类都是由它产生,Object 类拥有的方法和属性都会被其他类所继承。


属性:

名称描述
Constructor对创建对象的函数的引用(指针)。
Prototype对该对象的对象原型的引用。

方法:

名称描述
HasOwnPropety(property)判断对象是否具有某个属性。(例如,ob.HasOwnProperty("id"))
IsPrototypeOf(Object)判断对象是否为另一个对象的原型。
PropertyIsENumerable(property)判断给定的属性是否可以用 for…in 进行枚举。
ToString()返回一个对象内容转换为字符串的新值。
ValueOf()返回最适合对象的原始值。很多类返回值与 ToString() 返回的相同。


Number 类型

在 Javascript 开发中,可以表示 32 位整数,64 位浮点数,并且可以使用三种进制数(十进制、八进制和十六进制)表示数字。

十进制:默认,任意合法的数字均可以。

八进制:在数字前面加上数字 0,如十进制 56 用八进制表示为 070。

十六进制:在数字前面加上数字 0x,如十进制 31 用十六进制表示为 0x1f。


静态属性:

名称描述
Number.MIN_VALUEjs中最小的数字5e-324
Number.MAX_VALUEjs中最大的数字1.7976931348623157e+308
Number.POSITIVE_INFINITY正无穷Infinity
Number.NEGATIVE_INFINITY负无穷-Infinity
Number.NaN非数字(NaN,Not a Number)NaN

静态方法:

名称描述
isNaN()判断函数,判断参数是否是数字
isFinite()判断函数,判断数值是否超过了规定范围,如果没有超过,返回 true,超过了返回 false
Number()转换函数,将任意类型的数据转换成数值类型
parseInt()转换函数,将字符串类型的数据转换成整数
parseFloat()转换函数,将字符串类型的数据转换成浮点数

对象方法:

名称描述实例
.toString()将一个数字转换成字符串,可以用基模式

实例

      var num=10;
      alert(num.toString(2));		// 输出 "1010"
      alert(num.toString(8));		// 输出 "12"
      alert(num.toString(10));		// 输出 "10"
      alert(num.toString(16));		// 输出 "a"
      
.toFixed()小数格式化数字,返回具有指定位数小数的数字的字符串,转换过程使用四舍五入

实例

      var num1=99.4,num2=99.5;
      alert(num1.toFixed(0));		// 输出 99
      alert(num2.toFixed(0));		// 输出 100
      alert(num2.toFixed(1));		// 输出 99.5
      alert(num2.toFixed(2));		// 输出 99.50
      
.toExponential()指数格式化数字,返回科学计数法表示的数字的字符串,转换过程使用四舍五入

实例

      var num=998;
      alert(num.toExponential(1));		// 输出 1.0e+3
      alert(num.toExponential(2));		// 输出 9.98e+2
      alert(num.toExponential(3));		// 输出 9.980e+2
      
.toPrecision()精确格式化数字,返回指定位数(整数和小数)数字的字符串,转换过程使用四舍五入

实例

      var num=99;
      alert(num.toPrecision(1));		// 输出 1e+2
      alert(num.toPrecision(2));		// 输出 99
      alert(num.toPrecision(3));		// 输出 99.0
      alert(num.toPrecision(4));		// 输出 99.00
      


String 类型

常用类型,在合法字符和字符串两边加上一对单引号或双引号来表示 String 类型。

字符串类型在存储时是从 0 开始的,如 hello!,字符长度为6,存储顺序是 0 1 2 3 4 5。

另外,还有一些非打印的转义字符,像换行为 '\n',制表符 '\t'。


静态方法:

名称描述
fromCharCode(ascii)把 ascii 码转换成对应的字符

对象方法:

名称描述实例
.slice("start","end")从字符串中截取一段字符串,源字符串值不变

实例

      var str="www.houheaven.com";
      alert(str.slice(4));		// 输出 houheaven.com
      alert(str.slice(4,12));		// 输出 houheaven
      alert(str.slice(-4));		// 输出 .com
      alert(str);			// 输出	www.houheaven.com
      

当参数中出现负参数时,此时会加上字符串长度来处理。


.substring("start","end")从字符串中截取一段字符串,源字符串值不变

实例

      var str="www.houheaven.com";
      alert(str.substring(4));		// 输出 houheaven.com
      alert(str.substring(4,12));	// 输出 houheave
      alert(str.substring(-4));		// 输出 www.houheaven.com
      alert(str);			// 输出 www.houheaven.com
      

当参数中出现负参数时,此时会进行忽略处理。


.substr("start","length")从字符串中截取一段字符串,源字符串值不变

实例

      var str="www.houheaven.com";
      alert(str.substr(4,9));		// 输出 houheaven
      alert(str.substr(-4));		// 输出 .com
      alert(str);			// 输出 www.houheaven.com
      

当参数中出现负参数时,此时会加上字符串长度来处理。


.charAt(index)返回字符串中的单个字符,从 0 开始,以此类推

实例

      var str="Hello Eric";
      alert(str.charAt(0));		// 输出 H
      alert(str.charAt(1));		// 输出 e
      alert(str.charAt(2));		// 输出 l
      alert(str.charAt(3));		// 输出 l
      alert(str.charAt(4));		// 输出 o
      alert(str.charAt(5));		// 输出 
      alert(str.charAt(6));		// 输出 E
      alert(str.charAt(7));		// 输出 r
      alert(str.charAt(8));		// 输出 i
      alert(str.charAt(9));		// 输出 c
      
.charCodeAt(index)返回字符串中的单个字符的字符代码,从 0 开始,以此类推

实例

      var str="Hello Eric";
      alert(str.charCodeAt(0));		// 输出 72
      alert(str.charCodeAt(1));		// 输出 101
      alert(str.charCodeAt(2));		// 输出 108
      alert(str.charCodeAt(3));		// 输出 108
      alert(str.charCodeAt(4));		// 输出 111
      alert(str.charCodeAt(5));		// 输出 32
      alert(str.charCodeAt(6));		// 输出 69
      alert(str.charCodeAt(7));		// 输出 114
      alert(str.charCodeAt(8));		// 输出 105
      alert(str.charCodeAt(9));		// 输出 99
      
.concat("string")连接字符串,把一个或多个字符串连接在 String 对象的字符串上,原始值不变

实例

      var str1="Hello ";
      var str2="Eric";
      alert(str1.concat(str2));		// 输出 Hello Eric
      alert(str1);			// 输出 Hello
      

此函数的作用等同于字符串的直接连接(str1+str2)。


.indexOf("string","string")查找单字符,如果字符串中存在此字符,返回它在字符串中的位置,否则返回 -1,区分大小写

实例

      var str="www.houheaven.com";
      alert(str.indexOf("H"));			// 输出 -1
      alert(str.indexOf("h"));			// 输出 4
      alert(str.indexOf("heaven"));		// 输出 7
      
.lastIndexOf("string","string")查找单字符,功能同上,不同的是这个是从字符串末尾开始检索

实例

      var str="www.houheaven.com";
      alert(str.lastIndexOf("H"));			// 输出 -1
      alert(str.lastIndexOf("h"));			// 输出 7
      alert(str.lastIndexOf("hou"));			// 输出 4
      
.localeCompare("String")对字符串进行排序

实例

      var str1="white",str2="black",str3="Black";
      alert("white - white : "+str1.localeCompare(str1)+"\t");		// 输出 0
      alert("white - black : "+str1.localeCompare(str2)+"\t");		// 输出 1
      alert("black - Black : "+str2.localeCompare(str3)+"\t");		// 输出 -1
      

返回 0:String 对象与对比参数相等。

返回负数(-1):String 对象排在其对比参数之前。

返回正数(1):String 对象排在其对比参数之后。


.toUpperCase("String")英文字符串全部大写显示

实例

      var str="www.HouHeaven.com";
      alert(str.toUpperCase());		// WWW.HOUHEAVEN.COM
      
.toLowerCase("String")英文字符串全部小写显示

实例

      var str="www.HouHeaven.com";
      alert(str.toLowerCase);		// www.houheaven.com
      

html 方法:

名称描述
str.link(url)<a href="url">str</a>
str.anchor(name)<a name="name">str</a>
str.bold()<b>str</b>

这种方法是通过 js 生成 html 代码,还有很多类似方法,不做列举。




Boolean 类型

共有两个值,即 true 和 false。



Undefined 类型

Undefined 类型只有一个值,即 undefined。

当声明的变量未初始化时,该变量的默认值就是 undefined。



Null 类型

Null 类型也只有一个值,即 null,undefined 其实是 Null 派生出来的,二者相等,undefined 是声明了变量但是未赋值,null 是不存在这个对象。



【 Typeof 运算符】

说明:使用 typeof 运算符可以用来检测对象或变量的数据类型。

用法:typeof var 或者 typeof(var)

返回值:以上的几种数据类型


【 instance of 运算符】

说明:数组、对象、null 都属于对象,要想判断一个对象的具体类型,只能用这个方法。

显示框架
top