0%

Differences between var, let, and const?

✅ Scope

  1. Variables declared with var do not have block scope. They can be accessed outside blocks but not outside functions.
  2. Variables declared with let have block scope, accessible only within the declared block.
  3. const defines constants, must be initialized, accessible only in the declared block, and cannot be reassigned.

✅ Declaration Rules

  • A variable name can only be declared once using one of these methods; otherwise, it throws an error.

✅ Difference when using with this

Feature var / let / const
Can change this context ✅ Yes
First parameter is the object this refers to ✅ Yes
No object or undefined/null Defaults to global window
Parameter passing apply uses array, call uses parameter list, bind supports multiple arguments
Execution method apply/call execute immediately, bind returns a new function
What is event delegation?
  • Event delegation attaches events to a parent or ancestor element instead of individual child elements.
  • When the event reaches the target element, it bubbles up and triggers the handler on the outer element.
Difference between debounce and throttle?
  • Debounce: Executes only the last trigger. Even if the event keeps firing, it waits until n seconds after the last event to execute.
  • Throttle: Controls the execution frequency. Fires every n seconds during continuous event triggers.
How does frontend prevent SQL injection?
  1. Parameterized Queries: Use prepared statements or parameterized queries.
  2. Input Validation: Ensure user input follows expected format.
  3. Escape Special Characters: Escape characters when constructing SQL queries.
  4. Limit Permissions: Restrict user privileges to prevent unsafe operations.
  5. Whitelist Validation: Only allow expected values in input.
  6. Code Review: Regularly review code, especially DB-related parts.
Difference between HTTP and HTTPS
  1. HTTPS requires a certificate from a CA, which may cost money.
  2. HTTP transmits data in plaintext, while HTTPS uses SSL encryption.
  3. They use different connection methods and ports: HTTP uses 80, HTTPS uses 443.
  4. HTTP is stateless; HTTPS combines SSL + HTTP for encrypted, authenticated, secure communication.
Difference between created and mounted?
  • created runs before mounted. DOM is not fully rendered, but API requests can be made early here.
  • mounted is triggered when the DOM has been rendered, so it’s suitable for manipulating DOM elements.
Explain the event loop mechanism

🔁 What is the Event Loop?

JavaScript is single-threaded. In addition to the call stack, it relies on task queues to control asynchronous code execution order.

This whole process is called the Event Loop.

🧠 Core Concepts

  • Only one event loop in a single thread.
  • Multiple task queues:
    • Macro Tasks
    • Micro Tasks

⏱ Execution Order

Macro Task ➝ Clear all Micro Tasks ➝ Next Macro Task ➝ Clear all Micro Tasks…

  1. Execute a macro task (like the whole script)
  2. Execute all micro tasks created during this macro task
  3. If micro tasks generate more micro tasks, execute them too
  4. Begin the next macro task loop

🧩 Examples of Macro Tasks

  • script
  • setTimeout
  • setInterval
  • setImmediate (Node.js)
  • I/O operations
  • UI rendering

🧬 Examples of Micro Tasks

  • process.nextTick (Node.js, higher priority)
  • Promise.then / catch / finally
  • async / await
  • MutationObserver
Explain the CSS box model

The CSS box model includes IE and standard W3C models.

  • In standard W3C box model, width only includes content. box-sizing: content-box (default).
  • In IE box model, width includes content + padding + border. box-sizing: border-box.
  • box-sizing: padding-box includes left/right padding + width.
Differences between Vue 2 and Vue 3
  1. Two-way binding:
    • Vue 2: uses Object.defineProperty() (ES5), which only watches individual properties.
    • Vue 3: uses Proxy (ES6), which can observe entire objects and arrays.
  2. Lifecycle hooks:
    • Vue 2: beforeCreate, created, beforeMount, mounted, etc.
    • Vue 3: setup, onBeforeMount, onMounted, etc.
  3. Vue 2 requires a root tag; Vue 3 allows multiple root tags via Fragment.
  4. API:
    • Vue 2: Options API (functions and data handled separately).
    • Vue 3: Composition API (related code grouped together).
  5. Slots:
    • Named slot: Vue 2 uses slot="", Vue 3 uses v-slot="".
    • Scoped slot: Vue 2 uses slot-scope="data", Vue 3 uses #data or #default="{data}".

Installing ELK Stack (ElasticSearch, Logstash, Kibana) with Docker

It is recommended to install on Docker due to lower resource consumption. The installation version used below is 8.12.2. Please use the same version to avoid conflicts.

Table of Contents

1. Install ElasticSearch
2. Install Logstash
3. Install Kibana
Recommendations


1. Install ElasticSearch

  • Pull the image:
1
2
3
4
5
6
7
docker pull elasticsearch:8.12.2
````

* Start the container:

```bash
docker run --name some-elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -d elasticsearch:8.12.2
  • Enter the container:
1
sudo docker exec -u 0 -it some-elasticsearch bash
  • Restart the container:
1
docker restart some-elasticsearch

2. Install Logstash

  • Pull the image:
1
docker pull docker.elastic.co/logstash/logstash:8.12.2
  • Start the container:
1
sudo docker run -it -p 5044:5044 -p 9600:9600 --name logstash -v /usr/share/logstash/piplines:/usr/share/logstash/config --privileged=true docker.elastic.co/logstash/logstash:8.12.2 /bin/bash
  • Use scp to upload the MySQL Connector JAR file to the virtual machine:
1
scp "/Users/Downloads/logstash-8.12.2/mysql-connector-j-8.4.0.jar" username@VM-IP:/home
  • Move the JAR file from the VM to the Logstash container:
1
docker cp ./mysql-connector-j-8.4.0.jar logstash:/usr/share/logstash
  • Enter the container:
1
docker exec -u 0 -it logstash bash

3. Install Kibana

  • Pull the image:
1
docker pull kibana:8.12.2
  • Start the container:
1
docker run --name some-kibana -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -d elasticsearch:8.12.2
  • Enter the container:
1
docker exec -u 0 -it some-kibana bash

1
2
3
4
5
6
7
8
9
10
11
12
sudo apt update
sudo apt install docker.io docker-compose
docker -v
sudo systemctl start docker

sudo docker search portainer
docker pull portainer/portainer
sudo docker pull portainer/portainer
sudo docker run -d --name portainerUI -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock portainer/portainer

sudo docker start portainerUI
sudo passwd root

GitLab CI/CD Guide for Frontend Projects (Mac Environment)

Overview

In daily development, we often manually send the built dist package to the backend for deployment, which is inefficient and error-prone. CI (Continuous Integration) automates this process. This guide shows how to use GitLab CI to implement continuous integration and deployment.


1. Install GitLab Runner

Refer to the official documentation: GitLab Runner Installation Guide


2. Register Runner Locally

  1. Open your GitLab project page and go to Settings → CI/CD, expand the Runners section.
  2. Note the displayed URL and token for registration.

Run the registration command

1
gitlab-runner register

Follow the prompts and enter the following information:

  • GitLab CI Coordinator URL: https://gitlab.com
  • GitLab CI Token: xxx (copy from the project)
  • Tags: my-tag,another-tag (custom tags)
  • Description: my-runner (custom description)
  • Executor: shell (recommended on Mac)

After registration, return to the GitLab CI/CD settings page. If the runner status is green, it’s running successfully. If not, start the runner manually:

1
gitlab-runner run

3. Create and Commit .gitlab-ci.yml

Save the following content as .gitlab-ci.yml and push it to GitLab:

1
2
3
4
5
6
7
8
9
10
11
stages:
- deploy

deploy_to_test:
stage: deploy
script:
- yarn
- rm -rf dist/
- yarn build
- ls -l -t ./dist/
- rsync -avz ./dist/ root@xxx.xx.xx.xxx:/dist
  • This CI script builds the project and uses rsync to deploy the dist folder to a remote server.

4. CI with Docker (Optional)

If you want a more comprehensive CI/CD pipeline using Docker, follow these steps:

1. Install Docker (Mac)

1
brew install --cask docker

2. Pull GitLab Image

1
docker pull drud/gitlab-ce:v0.29.1

3. Create GitLab Container

1
docker run -d -p 8443:443 -p 8090:80 -p 8022:22 --restart always --name gitlab drud/gitlab-ce:v0.29.1

Explanation:

  • -p 8443:443: map HTTPS port
  • -p 8090:80: map HTTP port
  • -p 8022:22: map SSH port
  • --restart always: auto-restart on crash or reboot
  • --name gitlab: container named gitlab

Visit:

1
http://localhost:8090/

5. .gitlab-ci.yml Example for Docker Mode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
variables:
TEST_NAME: "tips"
OUT_PORT: "8081"
IN_PORT: "8081"

stages:
- deploy

deploy_to_test:
stage: deploy
before_script:
- if [ $(docker ps -aq --filter name=$CI_PROJECT_NAME) ]; then docker rm -f $CI_PROJECT_NAME; fi
script:
- docker build -f Dockerfile -t $TEST_NAME:latest .
- docker run -d -p $OUT_PORT:$IN_PORT --name $TEST_NAME $TEST_NAME:latest

After successful deployment, you can view the build process on the GitLab CI/CD Pipelines page. A new container will be generated under the Containers section. Click the corresponding port to access the deployed page.


This is an English post.