📄 2.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>JavaScript基本数据结构</title>
<meta name="Microsoft Theme" content="maize 011">
</head>
<body background="maizbk.jpg" bgcolor="#FFFFFF" text="#000000" link="#669999" vlink="#999900" alink="#3366CC">
<!--mstheme--><font face="宋体">
<table border="0" width="100%">
<tr>
<td width="100%"><strong>
<p align="center"><font face="宋体" lang="ZH-CN" size="4">JavaScript基本数据结构</font></p>
</strong>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN"> <font size="3">JavaScript提供脚本语言的编程与C++非常相似,它只是去掉了C语言中有关指针等容易产生的错误,并提供了功能强大的类库。对于已经具备C++或C语言的人来说,学习JavaScript脚本语言是一件非常轻松愉快的事。</font></font></span></p>
<b>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">一、JavaScript代码的加入</font></span></p>
</b>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"> JavaScript的脚本包括在HTML中,它成为HTML文档的一部分。与HTML标识相结合,构成了一个功能强大的Internet网上编程语言。可以直接将JavaScript脚本加入文档:</font></span></p>
<u><b>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"><Script
Language ="JavaScript"></font></span></b></u>
<dir>
<dir>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">JavaScript语言代码;</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">JavaScript
语言代码;</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">....</font></span></p>
</dir>
</dir>
<u><b>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"></Script></font></span></p>
</b></u>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">说明:</font></span>
<dir>
<dir>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"><img src="Bullet3.gif" width="12" height="12">
通过标识<Script>...</Script>指明JavaScript脚本源代码将放入其间。</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"><img src="Bullet3.gif" width="12" height="12">
通过属性<u><b>Language</b></u> ="JavaScript"说明标识中是使用的何种语言,这里是JavaScript语言,
表示在JavaScript中使用的语言。</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"> </font></span></p>
</dir>
</dir>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"> 下面是将JavaScript脚本加入Web文档中的例子:</font></span></p>
<u><b>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">Test2.html</font></span></p>
</b></u>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"><HTML></font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"><Head></font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"><Script
Language ="JavaScript"></font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">document.
Write("这是电脑报网络学校");</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">document.
close();</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"></Script></font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"></Head></font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"></HTML></font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">
在浏览器的窗口中调用test2.html,则显示“这是电脑报网络学校”字串。见图2所示。</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"><img src="Image26.gif" width="212" height="230"></font></span>
<dir>
<dir>
<dir>
<dir>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">图2</font></span></p>
</dir>
</dir>
</dir>
</dir>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">说明:</font></span>
<dir>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"><img src="Bullet3.gif" width="12" height="12"> Document.
write()是文档对象的输出函数,其功能是将括号中的字符或变量值输出到窗口;document.
close()是将输出关闭。</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"><img src="Bullet3.gif" width="12" height="12"> 可将<Script>...</Script>标识放入head>..
</Head>或<Body> ...</Body>之间。将JavaScript标识放置<Head>...
</Head>在头部之间,使之在主页和其余部分代码之前装载,从而可使代码的功能更强大;可以将JavaScript标识放置在<Body>...
</Body>主体之间以实现某些部分动态地创建文档。</font></span></p>
</dir>
<b>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"> </font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">二、基本数据类型</font></span></p>
</b>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"> JavaScript脚本语言同其它语言一样,有它自身的基本数据类型、表达式和算术运算符以及程序的基本框架结构。JavaScript提供了四种基本的数据类型用来处理数字和文字,
而变量提供存放信息的地方,
表达式则可以完成较复杂的信息处理。</font></span></p>
<b>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">1、基本数据类型</font></span></p>
</b>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"> 在JavaScript中四种基本的数据类型:数值(整数和实数)、字符串型(用“”号或‘’括起来的字符或数值)、布尔型(使True或False表示)和空值。在JavaScript的基本类型中的数据可以是常量,也可以变量。由于JavaScript采用弱类型的形式,因而一个数据的变量或常量不必首先作声明,而是在使用或赋值时确定其数据的类型的。当然也可以先声明该数据的类型,它是通过在赋值时自动说明其数据类型的。</font></span></p>
<b>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">2、常量</font></span></b>
<dir>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"><img src="Bullet3.gif" width="12" height="12"> 整型常量</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">JavaScript的常量通常又称字面常量,它是不能改变的数据。其整型常量可以使用十六进制、八进制和十进制表示其值。</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"><img src="Bullet3.gif" width="12" height="12"> 实型常量</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">实型常量是由整数部分加小数部分表示,如12.32、193.98
。可以使用科学或标准方法表示:5E7、4e5等。</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"><img src="Bullet3.gif" width="12" height="12"> 布尔值</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">布尔常量只有两种状态:True或False。
它主要用来说明或代表一种状态或标志,以说明操作流程。它与C++是不一样的,C++可以用1或0表示其状态,而JavaScript只能用True或False表示其状态。</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"><img src="Bullet3.gif" width="12" height="12"> 字符型常量</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">使用单引号(‘)或双引号(“)括起来的一个或几个字符。如
"This is a book of JavaScript "、"3245"、"ewrt234234"
等。</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"><img src="Bullet3.gif" width="12" height="12"> 空值</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">JavaScript中有一个空值null,表示什么也没有。如试图引用没有定义的变量,则返回一个Null值。</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"><img src="Bullet3.gif" width="12" height="12"> 特殊字符</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">同C语言一样,JavaScript中同样以有些以反斜杠(/)开头的不可显示的特殊字符。通常称为控制字符。</font></span></p>
</dir>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"> </font></span></p>
<b>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">3、变量</font></span></p>
</b>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"> 变量的主要作用是存取数据、提供存放信息的容器。对于变量必须明确变量的命名、变量的类型、变量的声明及其变量的作用域。</font></span>
<dir>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"><img src="Bullet3.gif" width="12" height="12"> 变量的命名</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">JavaScript中的变量命名同其计算机语言非常相似,这里要注意以下两点:</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">A、必须是一个有效的变量,即变量以字母开头,中间可以出现数字如test1、text2等。除下划线(-)作为连字符外,变量名称不能有空格、(+)、(-)、(,)或其它符号。</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">B、不能使用JavaScript中的关键字作为变量。</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">在JavaScript中定义了40多个类键字,这些关键是JavaScript内部使用的,不能作为变量的名称。如Var、int、double、true不能作为变量的名称。</font></span></p>
</dir>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"> 在对变量命名时,最好把变量的意义与其代表的意思对应起来,以免出现错误。</font></span>
<dir>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3"><img src="Bullet3.gif" width="12" height="12"> 变量的类型</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">在JavaScript中,变量可以用命令Var作声明:</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">var
mytest;</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">该例子定义了一个mytest变量。但没有赋予它的值。</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">Var
mytest=”This is a book”</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">该例子定义了一个mytest变量,
同时赋予了它的值。</font></span></p>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">在JavaScript中,变量以可以不作声明,而在使用时再根据数据的类型来确其变量的类型。</font></span></p>
</dir>
<p style="line-height: 150%"><span style="FONT-SIZE: 9pt"><font lang="ZH-CN" size="3">如:</font></span></p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -