if
语句的使用方法相同。DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
"use strict";
let score = 90;
if (score === 100) { // 第一个判断
alert("great");
} else if (score >= 60 && score < 100) { // 第二个判断
alert("pass");
} else { // 否则...
alert("fail");
}
script>
head>
<body>
body>
html>
while
循环语句的使用方法相同。DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
"use strict";
let age = 0;
while (age < 100) {
age = age + 1;
console.log(age);
}
script>
head>
<body>
body>
html>
do...while...
循环:"use strict";
let age = 0;
do {
age = age + 1;
console.log(age);
} while (age < 100);
for
循环语句的使用方法相同。DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
"use strict";
for (let i = 0; i < 100; i++) {
console.log(i);
}
script>
head>
<body>
body>
html>
forEach()
的使用方法参考:JavaScript Array forEach() 方法
在 JavaScript 中,我们可以使用forEach()
函数循环遍历数组中的元素。(ES 5.1)
例:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
"use strict";
let fruits = ["apple", "banana", "watermelon", "pear", "strawberry"];
fruits.forEach(function (value) {
console.log(value);
});
script>
head>
<body>
body>
html>
for...in
的使用方法参考:JavaScript For InforEach()
方法,在 JavaScript 中,我们还可以使用for...in
语句来循环遍历数组中的元素。DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
"use strict";
let fruits = ["apple", "banana", "watermelon", "pear", "strawberry"];
for (let index in fruits) {
console.log(fruits[index])
}
script>
head>
<body>
body>
html>
for...in
语句来循环遍历对象的属性。DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
"use strict";
let person = {
name: "张三",
age: 18,
gender: "男",
hobby: "打篮球"
}
for (let key in person) {
console.log(person[key])
}
script>
head>
<body>
body>
html>
for...of
语句也能遍历数组。使用方法参考:JavaScript For Of
例:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
"use strict";
let fruits = ["apple", "banana", "watermelon", "pear", "strawberry"];
for (let fruit of fruits) {
console.log(fruit)
}
script>
head>
<body>
body>
html>
Map
对象是键值对的集合。DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
"use strict";
// Map和Set ES6+
// 学生的名字,学生的成绩
// let names = ["Tom", "Jerry", "Spike"];
// let score = [100,90,80];
// 创建Map对象 - 保存键值对,并且能够记住键的原始插入顺序
let map = new Map([["Tom", 100], ["Jerry", 90], ["Spike", 80]]);
// get() - 通过key获得value
let score1 = map.get("Tom");
console.log(score1);
// set() - 为Map对象添加或修改一个键值对
map.set("Tuffy", 60); // 添加
console.log(map);
map.set("Jerry", 95); // 修改
console.log(map);
// delete() - 删除键值对
map.delete("Spike");
console.log(map);
script>
head>
<body>
body>
html>
Set
对象是值的集合。Set
集合中的元素只会出现一次,即集合中的元素是唯一的(可以去重~)。DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
"use strict";
// 创建Set对象 - 保存值,并且集合中的元素不重复,有序
let set = new Set([1, 1, 1, 1, 3, 4, 4, 4]);
console.log(set);
// add() - 添加元素
set.add(2);
console.log(set);
// delete() - 删除元素
set.delete(1);
console.log(set);
// has() - 判断Set对象是否包含某个元素
console.log(set.has(3));
script>
head>
<body>
body>
html>
for...of
语句来遍历Map
和Set
集合对象。(ES 6+)DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
"use strict";
let map = new Map([["Tom", 100], ["Jerry", 90], ["Spike", 80]]);
for (let student of map) {
console.log(student);
}
script>
head>
<body>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
"use strict";
let set = new Set([1, 1, 1, 1, 3, 4, 4, 4]);
for (let num of set) {
console.log(num);
}
script>
head>
<body>
body>
html>
function
关键字来定义函数。定义方式一
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
"use strict";
function abs(x) {
if (x >= 0) {
return x;
} else {
return -x;
}
}
script>
head>
<body>
body>
html>
retrun
,函数就将停止执行,返回结果。abs()
,分别传入一个正数和一个负数,查看结果:return
语句, 那么它的返回值为:undefined
。定义方式二
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
"use strict";
// 匿名函数 - 没有函数名。
// 将结果赋值给变量abs,通过abs就可以调用该匿名函数!
let abs = function (x) {
if (x >= 0) {
return x;
} else {
return -x;
}
}
script>
head>
<body>
body>
html>
abs()
中分别传入一个正数和一个负数,查看结果:abs()
中分别传入多个参数和不传递参数,查看结果:规避参数不存在的问题
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
"use strict";
let abs = function (x) {
// 手动抛出异常
if (typeof x != "number") { // typeof - 返回一个字符串,表示操作数的类型
throw "Not a Number";
}
if (x >= 0) {
return x;
} else {
return -x;
}
}
script>
head>
<body>
body>
html>
abs()
中传递任何参数,查看结果:Not a Number
。arguments
arguments
是一个对应于传递给函数的参数的类数组对象。它代表传递进函数的所有参数,是一个数组。参考:Arguments 对象 - JavaScriptDOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
"use strict";
let abs = function (x) {
console.log("x=>"+x);
for (let i = 0; i < arguments.length; i++) {
console.log(arguments[i]);
}
if (x >= 0) {
return x;
} else {
return -x;
}
}
script>
head>
<body>
body>
html>
abs()
函数传入多个参数,查看结果:剩余参数 - rest
...
标识。DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
"use strict";
let aaa = function (x, y, ...rest) {
console.log("x=>" + x);
console.log("y=>" + y);
console.log("rest=>" + rest);
}
script>
head>
<body>
body>
html>
aaa()
函数传入分别传入一个,两个以及多个参数,查看结果:var
定义的变量实际是有作用域的。局部变量
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
function abc() {
var x = 1;
x = x + 1;
}
x = x + 2; // Uncaught ReferenceError: x is not defined
script>
head>
<body>
body>
html>
Uncaught ReferenceError: x is not defined
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
function abc() {
var x = 1;
x = x + 1;
}
function xyz() {
var x = 10086;
x = x + 1;
}
script>
head>
<body>
body>
html>
abc()
和函数xyz()
都使用了x
作为变量名,但都在函数体内,所以两者的变量名不会发生冲突,程序能够正常运行,不会报错。DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
function abc() {
var x = 1;
function xyz() {
var y = x + 1; // 2
}
var z = y + 1; // Uncaught ReferenceError: z is not defined
}
script>
head>
<body>
body>
html>
console.log(z);
,查看结果:DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
function abc() {
var x = 1;
function xyz() {
var x = "A";
console.log("inner => " + x);
}
console.log("outer => " + x);
xyz(); // inner => A
}
abc(); // outer => 1
script>
head>
<body>
body>
html>
提示变量的作用域
y
的语句写在变量y
的声明与赋值语句之前:DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
function abc() {
var x = "x" + y;
console.log(x);
var y = "y";
}
abc(); // xundefined
script>
head>
<body>
body>
html>
y
的声明,但却不会提升变量y
的赋值。这是在 JavaScript 建立之初就存在的特性。DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
"use strict";
function xyz() {
var x = 10,
y = 3,
z,i; // undefined
z = x + y;
i = x - y;
console.log(z);
console.log(i);
}
xyz();
script>
head>
<body>
body>
html>
全局变量
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
"use strict";
var x = 1;
function f() {
console.log(x);
}
f();
console.log(x);
script>
head>
<body>
body>
html>
window
对象表示一个浏览器窗口或一个框架。在 JavaScript 中,window
对象是全局对象,所有的表达式都在当前的环境中计算。 也就是说在 JavaScript 中,默认所有的全局变量都会自动绑定在window
对象下。(函数本身也可以是变量)alert()
就可以写成window.alert()
;同样的,我们定义的全局变量x
也可以写作window.x
:DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
"use strict";
var x = 1;
// 以下三种写法的表示的效果是相同的
alert(x);
window.alert(x);
window.alert(window.x);
script>
head>
<body>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
"use strict";
var x = "xxx";
window.alert(x);
var old_alert = window.alert;
// old_alert(x);
window.alert = function () {
};
// 发现alert()失效了
window.alert(123);
// 恢复
window.alert = window.old_alert;
window.alert(456);
script>
head>
<body>
body>
html>
window
),任何变量(函数本身也可以作为变量),假设在函数作用范围内没有找到,就会向外查找。如果在全局作用域(window
)中都没有找到,那么就会报错:ReferenceError
(引用异常)。DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
window.alert(x);
script>
head>
<body>
body>
html>
规范
window
对象上。所以如果我们的项目中同时存在多个 js 文件,且它们之中使用了同名的全局变量,就会产生冲突。那么,我们应该如何减少这样的冲突呢?window
对象,把自己的代码全部放进自己定义的唯一的空间名字中,通过这种方式来减少全局命名冲突的问题( jQuery 就是使用的这种方式 )。例:DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
// 唯一全局变量
var MyApp = {};
// 定义全局变量
MyApp.name = "clown";
MyApp.add = function (a, b) {
return a + b;
}
script>
head>
<body>
body>
html>
for
循环体中使用var
定义变量,然后我们再在循环体外调用这个变量:DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
function aaa() {
for (var i = 0; i < 100; i++) {
console.log(i);
}
console.log(i + 1);
}
aaa();
script>
head>
<body>
body>
html>
var
定义变量时,即使除了循环体,我们也能调用到在循环体中定于的变量。这就说明发生了作用域冲突问题。let
。通过它,就能解决作用域冲突问题。它和var
的区别在于:var
定义的变量在其所在的整个函数体内都是可以被调用的,而let
定义的变量只在其所在的语句块里可以被调用。var
改为let
:DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
function aaa() {
for (let i = 0; i < 100; i++) {
console.log(i);
}
console.log(i + 1); // Uncaught ReferenceError: i is not defined
}
aaa();
script>
head>
<body>
body>
html>
let
定义循环体中的变量之后,循环体外的语句便不能再调用它了,作用域冲突问题得到了解决。let
来定义局部作用域中的变量。let
一样,const
也是 ES 6 推出的新特性。DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
// 在 ES6 推出之前,约定俗成,名字全部用大写字母表示的变量,就代表它为常量
var PI = 3.14;
PI = 2;
console.log(PI); // 这样定义的常量的值其实是可以被改变的
script>
head>
<body>
body>
html>
const
,用来定义常量(只读变量)。DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script>
// 在 ES6 推出了 const 关键字,用来定义只读变量(常量)
const PI = 3.14;
PI = 2; // Uncaught TypeError: Assignment to constant variable.
console.log(PI);
script>
head>
<body>
body>
html>
Uncaught TypeError: Assignment to constant variable.