Home > Web Front-end > JS Tutorial > Application of Node.js modules

Application of Node.js modules

黄舟
Release: 2017-01-17 15:52:24
Original
1182 people have browsed it

Node.js module

Create module

Define module: module.exports = {}

Use module: require(module name)

require searches for modules: File module cache area–> Native module–> File loading–> Cache file module

Application of Node.js modules

Case:

hello.js —Define module

[code]function Hello () {
    var name;
    this.setName = function (aName) {
        name = aName;
    };
    this.getName = function () {
        console.log('Hi, ' + name);
    }
}
module.exports = Hello;
Copy after login

getHello.js—Introduce module

[code]var Hello = require('./hello.js');
var hello = new Hello();
hello.setName('张三');
hello.getName();
Copy after login

Application of Node.js modules

The above is the content of the application of Node.js module. For more related content, please pay attention to PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template