JavaScript 是一种广泛使用的编程语言,尤其在网页开发中不可或缺。以下是核心知识点的简明指南:

基础语法 🧩

  • 变量声明:使用 letconst 替代旧版 var
    let message = "Hello, World!";
    const PI = 3.1415;
    
  • 数据类型:包括字符串、数字、布尔值、数组、对象等
    typeof null; // "object"(历史遗留行为)
    
  • 注释:单行用 //,多行用 /* ... */
    // 这是单行注释
    /*
     * 这是多行注释
     * 可以跨多行
     */
    
JavaScript_语法

函数与作用域 🚀

  • 函数定义:使用 function 关键字或箭头函数
    function add(a, b) { return a + b; }
    const subtract = (a, b) => a - b;
    
  • 作用域规则letconst 声明的变量具有块级作用域
    if (true) {
      let x = 10; // 仅在 if 块内有效
    }
    
  • 闭包:函数可以访问并记住其词法作用域
    function createCounter() {
      let count = 0;
      return () => count++;
    }
    
函数_定义

对象与原型 🧬

  • 对象创建:使用字面量或 new Object()
    const user = { name: "Alice", age: 25 };
    
  • 原型链:通过 prototype 属性继承方法
    function Person() {}
    Person.prototype.greet = function() { console.log("Hi!"); };
    
  • ES6 类:语法糖封装原型功能
    class Animal {
      constructor(name) { this.name = name; }
      speak() { return `${this.name} makes a noise.`; }
    }
    
对象_原型

异步编程 ⏳

  • 回调函数:传统异步处理方式
    setTimeout(() => {
      console.log("This is a callback");
    }, 1000);
    
  • Promise:链式调用处理异步操作
    fetch("https://api.example.com/data")
      .then(response => response.json())
      .then(data => console.log(data));
    
  • async/await:更直观的异步代码写法
    async function getData() {
      const response = await fetch("https://api.example.com/data");
      return await response.json();
    }
    
异步_编程

扩展阅读 🔍

如需深入学习 JavaScript 高级特性,可访问 JavaScript 高级教程 获取更多内容。