JavaScript control statements and building a front-end server

Table of contents

一、for in

two, for of

Three, try catch

Fourth, build a front-end server

(1) Install nvm

(2) Check npm

(3) Build a front-end server


一、for in

Mainly used to traverse objects

let father = {name:'张三', age:18, study:function(){}};

for(const n in father) {
    console.log(n);
}
  • Where const n represents the property name traversed

  • Note 1: The method name can also be traversed (it is actually a special attribute)

  • Note 2: When traversing the child object, the properties of the parent object will be traversed

let son = Object.create(father);
son.sex = "男";

for(const n in son) {
    console.log(n);
}
  • Note 3: To get the attribute value in for in, use the [] syntax instead of the . syntax

for(const n in son) {
    console.log(n, son[n]);
}

two, for of

It is mainly used to traverse the array, and it can also be other iterable objects, such as Map, Set, etc.

let a1 = [1,2,3];

for(const i of a1) {
    console.log(i);
}

let a2 = [
    {name:'张三', age:18},
    {name:'李四', age:20},
    {name:'王五', age:22}
];

for(const obj of a2) {
    console.log(obj.name, obj.age);
}

for(const {name,age} of a2) {
    console.log(name, age);
}

Three, try catch

let stu1 = {name:'张三', age:18, address: {city:'北京'}};
let stu2 = {name:'张三', age:18};

function test(stu) {
    try {
        console.log(stu.address.city)   
    } catch(e) {
        console.log('出现了异常', e.message)
    } finally {
        console.log('finally');
    }
}

Fourth, build a front-end server

(1) Install nvm

nvm is (node ​​version manager), the advantage is that it is convenient to switch node.js version

Installation Precautions:

  1. To uninstall the existing nodejs

  2. When prompted to select the nvm and nodejs directories, be sure to avoid spaces in the directories

  3. Select [run as administrator] cmd program to execute nvm command

  4. Set the domestic mirror address before the first run

nvm node_mirror http://npm.taobao.org/mirrors/node/
nvm npm_mirror https://npm.taobao.org/mirrors/npm/

First check what versions are available

nvm list available

output:

 It is recommended to install LTS (Long Term Support)

nvm install 16.16.0

Executing nvm list will list the installed version

switch to 16.16.0

nvm use 16.16.0

After installation, nvm's own environment variables will be added automatically, but you may need to manually add the PATH environment variable of nodejs

(2) Check npm

Npm is a js package manager, similar to maven in the java world, make sure it uses a domestic image

Check mirror:

npm get registry

If the return is not https://registry.npm.taobao.org/, you need to do the following settings:

npm config set registry https://registry.npm.taobao.org/

(3) Build a front-end server

Create a new client folder to save the project, enter the folder and execute:

npm install express --save-dev

Modify the package.json file:

{
  "type": "module",
  "devDependencies": {
    "express": "^4.18.1"
  }
}
  • Where devDependencies are added by npm install --save-dev

 Execute js code (run the front-end server)

node main.js

Guess you like

Origin blog.csdn.net/m0_61961937/article/details/130223120