1 基本语法
1.1 变量与数据类型
// 变量使用let定义
// 数据类型包括:number、boolean、string、null、underfine(定义了未赋值)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<script>
let num = prompt("输入一个数")
console.log(num % 4 === 0 && num %100 !== 0)
console.log("a" && 1)
alert("弹出")
</script>
</body>
</html>
1.2 if
if (条件){
} else if (条件2) {
} else {
}
1.3 三元运算符
条件 ? 条件满足执行 : 条件不满足执行
1.4 循环语句
while (循环条件){
}
1.5 for
for (let i=1; i<100; i++){
}
1.6 数组
let ms = new Array()
let arr = [1, 'a', 2, 3]
alert(arr.pop())
arr.splice(0, 2)
let n = arr.push('b')
alert(n)
let m = arr.unshift('c')
alert(m)
for (let i=0; i<arr.length; i++){
console.log(arr[i])
}
let ms = new Array()
for (let i=0; i<matrix.length; i++){
ms[i] = new Array()
for (let j=0; j<matrix[0].length; j++){
ms[i][j] = matrix[i][j]
}
}
console.log(ms)
1.7 函数
function f(参数列表){
return 1
return [1, 2]
}
f(参数)
1.8 匿名函数
let f = function () {
}
function g(形参){}(实参)
1.9 对象
let 对象名 = {
属性名: 属性值,
方法名: 函数(匿名函数)
}
console.log(对象名)
对象名.属性名
对象名['属性名']
对象名.属性名 = 新值
对象名.新属性名 = 值
delete 对象名.属性名
对象名.方法名(参数)
for (let k in 对象名){
console.log(k + 对象名[k])
}
function f(){
let a = {
k: 10,
s: "哈哈",
g: function(x){
console.log(x)
}
}
console.log(a)
a.k = 100
a.x = 12
delete a.s
console.log(a["k"])
console.log(a.s)
console.log(a.x)
a.g('aaaaaa')
for (let k in a){
console.log(k + a[k])
}
}
f()
1.10 内置对象Math
Math.random 生成0~1的随机数
Math.ceil 向上取整
Math.floor 向下取整
Math.max
Math.min
Math.pow 幂运算
Math.abs 绝对值
function f(n, m){
console.log(Math.pow(10, 2))
console.log(Math.random())
console.log(Math.floor(Math.random()*(m-n+1))+n)
}
f(1, 10)
1.11 字符串操作
let s = 'abc'
s.chatAt(0)
s.indexOf('a')
s.includes('b')
1.12 异常捕获
try {
} catch(e){
} finally{
}
1.13 类
class 类名 extends 父类{
constructor(参数1, 参数2) {
this.参数1 = 参数1
this.参数2 = 参数2
}
方法1() {
}
方法2() { ... }
}
变量名 = new 类名(实参)