# 基础语法

js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
document.write('直接改变页面')

alert('弹出弹窗,显示信息');

let n = prompt('弹出弹窗,显示输入框')
// let 声明变量,省略 let 是隐式声明,不推荐

const timeButton = document.getElementById('timeButton')
// const 声明常量

document.write(typeof 1)
// typeof 检查数据类型

console.log(typeof 1)
// 输出到浏览器控制台


for (let i = 1; i < 10; i++) {
for (let j = 1; j < 10; j++)
document.write(`<div> ${i} * ${j} = ${i * j} </div>`)
document.write('<br>')
}
// 循环打印
// for、while、switch 和 c++ 相同

let arr = [1, '2', true]
console.log(arr.length)

arr.push(1) // 向数组末尾添加元素
arr.pop() // 删除数组末尾元素
arr.unshift(1) // 向数组头部添加元素
arr.shift() // 删除数组头部元素

function hello(a) {
console.log(a)
return 1
}

hello("666")

let fn = function () {
console.log('隐匿函数')
return 1
}

# 对象函数

js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// 对象
let person = {
name: 'John',
age: 30,
city: 'New York'
};

// 访问对象的属性
console.log(person.name);
console.log(person['name'])

// 动态添加属性
person['city'] = 'New York';

// 方法定义
let dx = {
hi: function () {
console.log('hi');
}
}

// 方法调用
dx.hi()

// 动态添加方法
dx.sad = function () {
console.log('sad');
}

// 遍历对象
for (let key in person) {
console.log(key);
console.log(person[key]);
}

// ...省略参数
let fc = function (a, ...b) {
for (let i = 0; i < b.length; i++)
console.log(b[i])
}

// 箭头函数
const fn = () => {
console.log('hello world');
}