前言
这里只是简单列出Javascript的知识点
一、基础语法
1.1 变量声明
1 2 3 4 5 6
| var name = "张三";
let age = 25; const PI = 3.14;
|
1.2 数据类型
| 类型 |
说明 |
示例 |
Number |
数字 |
42, 3.14 |
String |
字符串 |
"Hello", 'World' |
Boolean |
布尔值 |
true, false |
Null |
空值 |
null |
Undefined |
未定义 |
undefined |
Symbol |
唯一值(ES6) |
Symbol('id') |
BigInt |
大整数(ES2020) |
123n |
Object |
对象 |
{name: "张三"} |
1.3 类型检测
1 2 3 4 5 6
| typeof 42 typeof "text" typeof true typeof undefined typeof null Array.isArray([])
|
二、运算符
2.1 算术运算符
2.2 比较运算符
1 2 3 4 5
| == === != !== > < >= <=
|
2.3 逻辑运算符
2.4 赋值运算符
1
| = += -= *= /= %= **= &&= ||= ??=
|
三、流程控制
3.1 条件语句
1 2 3 4 5 6 7 8 9 10 11 12
| if (age > 18) { console.log("成年人"); } else { console.log("未成年人"); }
switch(day) { case 1: console.log("周一"); break; default: console.log("周末"); }
|
3.2 循环语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| for (let i = 0; i < 5; i++) { console.log(i); }
while (condition) { }
for (const item of array) { console.log(item); }
for (const key in object) { console.log(key); }
|
四、函数
4.1 函数定义
1 2 3 4 5 6 7 8 9 10 11 12
| function sum(a, b) { return a + b; }
const multiply = function(a, b) { return a * b; };
const divide = (a, b) => a / b;
|
4.2 函数参数
1 2 3 4 5 6 7 8 9
| function greet(name = "Guest") { console.log(`Hello, ${name}!`); }
function sum(...numbers) { return numbers.reduce((a, b) => a + b); }
|
4.3 高阶函数
1 2 3 4 5 6 7 8 9
| function fetchData(callback) { setTimeout(() => callback("数据"), 1000); }
function createAdder(x) { return y => x + y; }
|
五、对象与类
5.1 对象创建
1 2 3 4 5 6 7 8 9 10 11 12 13
| const person = { name: "张三", age: 30, greet() { console.log(`我是${this.name}`); } };
function Person(name) { this.name = name; }
|
5.2 类(ES6)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Animal { constructor(name) { this.name = name; } speak() { console.log(`${this.name} makes a noise`); } }
class Dog extends Animal { speak() { super.speak(); console.log(`${this.name} barks`); } }
|
六、数组操作
6.1 常用方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| const arr = [1, 2, 3];
arr.forEach(item => console.log(item));
const doubled = arr.map(x => x * 2);
const evens = arr.filter(x => x % 2 === 0);
const found = arr.find(x => x > 1);
arr.sort((a, b) => a - b);
|
6.2 扩展运算符(ES6)
1 2
| const newArr = [...arr, 4, 5]; const objCopy = {...originalObj};
|
七、异步编程
7.1 Promise
1 2 3 4 5 6 7
| const promise = new Promise((resolve, reject) => { setTimeout(() => resolve("成功"), 1000); });
promise .then(result => console.log(result)) .catch(error => console.error(error));
|
7.2 async/await(ES2017)
1 2 3 4 5 6 7 8 9
| async function fetchData() { try { const response = await fetch('url'); const data = await response.json(); console.log(data); } catch (error) { console.error(error); } }
|
八、模块系统(ES6)
8.1 导出
1 2 3 4 5 6
| export const name = "张三"; export function sayHi() { }
export default class Person { }
|
8.2 导入
1 2
| import { name, sayHi } from './module.js'; import Person from './Person.js';
|
九、现代JS特性
9.1 解构赋值(ES6)
1 2
| const [a, b] = [1, 2]; const {name, age} = person;
|
9.2 可选链(ES2020)
1
| const street = user?.address?.street;
|
9.3 空值合并(ES2020)
1
| const value = input ?? "default";
|
十、最佳实践
- 使用严格模式(
"use strict")
- 优先使用const/let替代var
- 使用===代替==
- 避免全局变量污染
- 函数单一职责原则
- 错误处理(try/catch)
- 代码模块化组织
- 编写可读性高的代码
- 使用ESLint规范代码
- 编写单元测试
注:JavaScript是动态类型的解释型语言,遵循ECMAScript标准,本文涵盖ES5到ES2022主要特性。
版权声明: 此文章版权归曦曦所有,如有转载,请注明来自原作者