node (writing structured programs)

   Use of node.js

console.log('Xiao Zou is the darkest');

// Use the js file to execute the NodeJS code (master):

// 1. In the code folder, [new js file] (do not create a new html), write any js code in it.

// const skill = 'Meow Meow Boxing'

// console.log('my skills'+ skill);

// 2. Come to the code folder, enter [cmd] in the above path, and a small black window will pop up.

// 3. Enter [node file name] to execute the code.

// node file name

// Skill:

// node -v: View the node download version.

// The file name is too long, you can enter half of it, and press the tab key to make up.

// Press the arrow key ↑ to switch to the last typed command.

// Run node, write code directly in the terminal (understand):    

// This method does not require JS files, enter node in any small black window, and enter the node execution environment.

// At this time, any JS code can be written.

 modular concept

necessity:

     In the early stage of JS development, simple page interaction logic can be implemented with a few words  

     With the advent of the big front-end era, the front-end code is expanding day by day. At this time, JS will consider using modular specifications to manage  

 concept:  

    Split a large program into n small files that depend on each other

    These small files can also be grouped together with a specific syntax

    This process is called modularization  

    advantage:  

    Avoid naming conflicts (reduce namespace pollution)

    Better separation, load on demand  

    Higher reusability    

    high maintainability    

    shortcoming:  

    no disadvantages

    : Need to learn modular syntax  

modular specification

There are three main categories of modules in Node.js:

custom module

self-written module

In NodeJS, the created JS files are all custom modules. (Everything is a module)

Built-in modules (core modules)

 After installing Node, it comes with many built-in modules, we can load and use them directly.

third party module

 Modules written by others are published on the npm website, and we can download and use them.

     [Front-end modular specification]

     AMD(Asynchronous Module Definition): Asynchronous module definition specification.    

Specifically for browsers, modules are loaded asynchronously.

AMD mainly achieves modularity through require.js. (Through the define method, the code is defined as a module; through the require method, the module loading of the code is realized.)

At present, it is rarely used, very little, just understand it.

// 在require.js中,使用require.js提供的函数require()来应用一个模块 
     require(['模块文件的路径(不带.js后缀)',function(){}
//模块加载成功之后的回调函数
])

//	CMD(Common Module Definition):公共模块定义规范。	
// CMD是另一种JS模块化方案,它与AMD很类似。
// 不同点在于:AMD推崇依赖前置、提前执行:CMD推崇依赖就近,延迟执行。
//CMD主要通过sea.js实现此规范。// 目前看到的很少,很少,了解即可。


// 使用define函数,传入的一般是一个函数,这个函数接受三个参数,分别是require、exports、module。
 define(function(require,exports,module){
const a = require('a')//同步导入
const b = require.async('b',function(b){
console.log(b);
}) 
//异步导入
})

CommomJS

Node.js is the main practitioner of CommonJS, and CommonJS is built into Node.js.

 The next modular syntax we learn is Common.js.

[Overview]

 Each file is a module and has its own scope. Variables, functions, and classes defined in a file are all private and invisible to other files.

On the server side, modules are loaded synchronously at runtime. On the browser side, modules need to be compiled and packaged in advance.

[Features]

All code runs in the module scope, without polluting the global scope.

 The module can be loaded multiple times, but it will only be run once in the first loading, and then the running result will be cached, and the cached result will be read directly when it is loaded later.

 The order in which modules are loaded, in the order in which they appear in the code.

[grammar]

exposed module

module exports = value

exports xxx = value

 import module

require(xxx)

In third-party modules, xxx is the module name. In a custom module, xxx is the module file path.

     ESM (commonly used, JS official syntax, used in vue): ES6 Module  

ES6 implements module functions at the level of language standards, and will gradually become a common module solution for browsers and servers.

Export module: export

Import module: import

index

console.log('这是一段代码,需要引入别的模块的内容');

// 导入模块
// const 变量 = require(‘路径’)
const  order =  require ('./order   ')


console.log(order);


console.log(order.goodsName);
console.log(order.money);
console.log(order.info);



order.red('小邹怎么减肥')

order

const money = 9.9
const goodsName = '小黄有点虚'
const info = '把肾割了买iphone'

function read(book){
    console.log('读' + book);
}

// 暴露模块
// module.exports = 暴露的数据(一般暴露一个对象)
module.exports = {

    // 暴露对象出去
    money,
    goodsName,
    info
}

 

Guess you like

Origin blog.csdn.net/m0_73495603/article/details/127394621