- ECMAScript:核心 解释器 0 1 -没有不兼容问题
- DOM(Document Object Model)文档对象模型,操作html的能力 document -有些不兼容
- BOM:(Browser Object Model)浏览器对象模型 操作浏览器 window -完全不兼容
JavaScript提供七种不同的data types(数据类型),它们是undefined
(未定义), null
(空), boolean
(布尔型), string
(字符串), symbol
(符号), number
(数字), and object
(对象)
JavaScript一种直译式脚本语言,是一种动态类型、弱类型、基于原型的语言,内置支持类型。它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在HTML(标准通用标记语言下的一个应用)网页上使用,用来给HTML网页增加动态功能。
通过在变量的前面使用关键字var
,我们告诉 JavaScript 来创建或者 declare(声明)一个变量,就像这样:
var a=12;
我们可以通过typeof确定变量类型
<script>
var a =12;
alert(typeof a);//number
a='adufjhw';
alert(typeof a);//string
a=true;
alert(typeof a);//boolean
a=function(){
alert('fddj');
}
alert(typeof a);//function
a=document;
alert(typeof a);//object
var b;
alert(typeof b);//undefined
//没有定义,
//虽然定义了 但是未赋值
加法运算 parseint()
<html>
<head>
<title>加法 </title>
<script>
function add(){
var a=document.getElementById('txt1');
var b=document.getElementById('txt2');
oBtn.onclick=function(){
alert(parseInt(a.value)+parseInt(b.value));
}
}
</script>
</head>
<body>
<input id='txt1' type="text"/>+
<input id='txt2' type="text"/>
<button id='oBtn' onclick="add();">计算结果</button>
</body>
</html>
完善版本
<html>
<head>
<title>加法 </title>
<script>
function add(){
var a=document.getElementById('txt1');
var b=document.getElementById('txt2');
oBtn.onclick=function(){
var n1=parseInt(a.value);
var n2=parseInt(b.value);
if(isNaN(n1)){
alert("您输入的第一个数字有误");
}
else if(isNaN(n2)){
alert("您输入的第二个数字有误");
}
else{
alert(n1+n2);
}
}
}
</script>
</head>
<body>
<input id='txt1' type="text"/>+
<input id='txt2' type="text"/>
<button id='oBtn' onclick="add();">计算结果</button>
</body>
</html>
小数 parseFloat()
隐式转换和显式转换
<html>
<head>
<title>加法 </title>
<script>
var a=5;
var b='5';
alert(a==b);//true 先转换类型,再比较
alert(a===b);//false 不转换类型直接比较
alert(a+b);//字符串链接
alert(a-b);//数字相减
</script>
</head>
<body></body>
</html>
摄氏度转华氏度的小应用。
从Celsius
摄氏度转换为Fahrenheit
华氏度的算法是:摄氏度的温度乘于9除于5,再加上32。
在 JavaScript 中,你可以通过在引号前面使用 反斜杠 (\
) 来转义引号
常见的转义序列列表:
Code | Output |
\' | 单引号 |
\" | 双引号 |
\\ | 反斜杠符 |
\n | 换行符 |
\r | 回车符 |
\t | 制表符 |
\b | 退格符 |
\f | 换页符 |
通过在字符串变量或字符串后面写上 .length
来获得字符串变量 字符串
值的长度。
"Alan Peter".length; // 10
例如,我们创建了一个变量 var firstName = "Charles"
,我们就可以通过使用 firstName.length
来获得 "Charles"
字符串的长度。
通过[索引]
来获得对应的字符。
大多数现代编程语言,如JavaScript,不同于人类从1开始计数。它们是从0开始计数,这被称为 基于零 的索引。
例如, 在单词 "Charles" 中索引0上的字符为 "C",所以在 var firstName = "Charles"
中,你可以使用 firstName[0]
来获得第一个位置上的字符。