Chi's xu

website·Indexed 30 days ago

https://carina957.github.io

A personal knowledge base and guide covering frontend development, including React, Vue, TypeScript, JavaScript, Nuxt, Webpack, and CSS. It also features technical documentation on Git, Node.js, VSCode configuration, interview preparation (algorithms and system internals), and personal notes on reading, poetry, and life experiences.

Tokens
38.8K
Snippets
154
Records
189
Agent score
75%

What's inside Chi's xu

  1. Implement object interception using JavaScript Proxy

    The Proxy object allows you to create a wrapper for a target object, intercepting and redefining fundamental operations (like getting or setting properties).
    // Syntax: const proxy = new Proxy(target, handler)
    const obj = new Proxy(target, handler);
  2. Configure HTTP request bodies for data transmission

    When using methods like POST or PUT, data is transmitted in the request body. For modern Web applications using REST architecture with JSON data, the Content-Type header must be set to application/json to inform the server of the data format.
    Content-Type: application/json
  3. Inspect system and binary environment

    Use envinfo via npx to output detailed information about the current OS, Node.js version, package managers, and installed browsers.
    npx envinfo --system --binaries --browsers
  4. Use ni to abstract package manager commands

    The ni tool allows you to run installation and run commands without worrying about whether the project uses npm, yarn, pnpm, or bun. It automatically detects the correct package manager based on the lockfile.
    # Install ni globally
    npm i -g @antfu/ni
    
    # Install dependencies (npm install / yarn install / pnpm install / bun install)
    ni
    
    # Uninstall a package (npm uninstall / yarn remove / pnpm remove)
    nun @types/node
    
    # Add a package (npm install / yarn add / pnpm add)
    ni vite
    
    # Add a dev dependency (npm i -D / yarn add -D / pnpm add -D)
    ni @types/node -D
    
    # Install a global package (npm install -g / yarn add global / pnpm add -g)
    ni -g eslint
    
    # Clean install / frozen lockfile (npm ci / yarn install --frozen-lockfile / pnpm install --frozen-lockfile)
    ni --frozen
    # or
    nci
    
    # Upgrade packages (npm upgrade / yarn upgrade / pnpm update)
    nu
    
    # Interactive upgrade (yarn upgrade-interactive / pnpm update -i)
    nu -i
  5. Understand Vue's progressive framework philosophy

    Vue is designed as a progressive framework, meaning it focuses on minimal core responsibilities without imposing strict architectural constraints. This allows developers to use it flexibly: as a lightweight view layer (similar to jQuery) for specific components within an existing system, or as a full-featured framework (similar to Angular) using the entire ecosystem. It allows the underlying business logic to be implemented using either Object-Oriented (OO) design patterns or functional programming.
  6. Understand the browser request lifecycle from URL entry to page render

    When a user enters a URL in a browser, the following high-level sequence of events occurs to retrieve and display the page:

    1. DNS Resolution: Converting the human-readable domain name into an IP address.
    2. TCP Connection: Establishing a reliable connection between the client and server.
    3. HTTP Request: Sending the request for the specific resource.
    4. Server Processing: The server processes the request and returns an HTTP response.
    5. Browser Rendering: The browser parses and renders the returned content.
    6. Connection Termination: Closing the network connection.
  7. Understand JSX syntax and compilation in React

    JSX is a syntax extension for JavaScript used in React. It allows developers to write HTML-like structures within JavaScript, which are then transpiled by Babel into React.createElement() function calls.

    Key benefits of JSX include:

    • Performance: Optimized after compilation into JavaScript.
    • Type Safety: Errors can be detected during the compilation process.
    • Developer Experience: Simpler and faster template authoring.

    When JSX is compiled, it results in a JavaScript object containing the type of the element and its props (including children).

    // JSX Syntax
    const element = (
      <div className="greeting">
        <h1>Hi there 👋</h1>
      </div>
    );
    
    // Equivalent React.createElement call
    const elementR = React.createElement(
      'div',
      { className: 'greeting' },
      React.createElement('h1', null, 'Hi there 👋')
    );
    
    // Resulting object structure (simplified)
    const elementObj = {
      type: 'div',
      props: {
        className: 'greeting',
        children: { type: 'h1', props: { children: 'Hi there 👋' } },
      },
    };
  8. Implement a reactive data listener using Proxy

    The Proxy object allows you to create a wrapper around a target object to intercept and redefine fundamental operations (like getting, setting, or deleting properties). Unlike Object.defineProperty, which requires immediate recursive traversal for deep listening, Proxy implements lazy deep listening by recursively wrapping nested objects only when they are accessed via the get trap. This improves initial performance for deeply nested data structures.
    // Create a reactive proxy
    function reactive(target = {}) {
      if (typeof target !== 'object' || target === 'null')
        return target // Return directly if not an object or array
    
      // Proxy configuration (traps)
      const proxyConf = {
        get(target, key, receiver) {
          // Only handle own properties (non-prototype)
          const ownKeys = Reflect.ownKeys(target)
          ownKeys.includes(key) && console.log(/get/, key) // Log access
    
          const result = Reflect.get(target, key, receiver)
    
          // Lazy recursive call: only wrap nested objects when accessed
          return reactive(result)
        },
        set(target, key, val, receiver) {
          // Do not process if value is identical
          if (val === target[key])
            return true
    
          const ownKeys = Reflect.ownKeys(target)
    
          if (ownKeys.includes(key))
            console.log(/set existing property/, key)
          else 
            console.log(/set new property/, key)
    
          const result = Reflect.set(target, key, val, receiver)
          console.log(/set/, key, val)
    
          return result // Return success boolean
        },
        deleteProperty(target, key) {
          const result = Reflect.deleteProperty(target, key)
          console.log(/deleteProperty/, key)
          return result
        },
      }
    
      // Generate the proxy object
      return new Proxy(target, proxyConf)
    }
    
    // Test data
    const data = {
      name: 'Jack',
      age: 24,
      info: {
        city: 'hangzhou',
        a: { b: { c: { d: 1 } } },
      },
    }
    const observed = reactive(data);
  9. Distinguish traditional Chinese architectural structures

    Traditional Chinese architecture uses specific terms to describe different types of pavilions and halls based on their function and design:

    • Ting (亭): For resting and viewing.
    • Tai (台): High, flat platforms for rituals or viewing.
    • Lou (楼): Large buildings with two or more stories for living or viewing.
    • Ge (阁): Small towers with windows on all sides, used for climbing or storing books.
    • Xuan (轩): High, open buildings for viewing and resting.
    • Xie (榭): Buildings constructed by the water to integrate with water scenery.
    • Lang (廊): Covered walkways connecting buildings.
    • Fang (舫): Buildings designed to mimic the shape of a boat.
  10. Understand browser refresh behaviors and cache usage

    Different ways of refreshing a page trigger different caching behaviors:

    • URL Enter + Enter: The browser checks the cache first. If a valid (non-expired) version exists, it loads from cache immediately without a server request.
    • F5 (Refresh): The browser ignores strong cache and performs a negotiated request (e.g., sending If-Modified-Since) to check if the resource has expired.
    • Ctrl + F5 (Hard Refresh): The browser clears the local cache for that resource and performs a full request to the server to fetch a fresh copy.
  11. Initialize a new Git repository

    To start a new Git project from scratch, initialize the repository, set the default branch to 'main', create an initial file, and push it to a remote origin.
    git init
    git branch -m main
    echo '# Hello Git!' > README.md
    git add .
    git commit -m 'First Commit'
    git remote add origin git@github.com:xxx
    git push -m origin main
  12. Use number-precision for complex floating-point math

    For comprehensive floating-point precision management, use the number-precision npm package.
    $ npm i numer-precision -S
    NP.strip(num) // strip a number to nearest right number
    NP.plus(num1, num2, num3, ...) // addition
    NP.minus(num1, num2, num3, ...) // subtraction
    NP.times(num1, num2, num3, ...) // multiplication
    NP.divide(num1, num2, num3, ...) // division
    NP.round(num, ratio) // round a number based on ratio