zoukankan      html  css  js  c++  java
  • 模块接口

    module.exports的使用

     test.js

     1 'use strict';
     2 var config = {
     3     database: 'dbname',
     4     username: 'name',
     5     password: 'pwd',
     6     host: 'localhost',
     7     port: 3306
     8 };
     9 function hello() {
    10     console.log('Hello, world');
    11 }
    12 function greet(name){
    13     console.log("Hello,"+name);
    14 }
    15 //方式一
    16 //只调用一个,此方法适用于调用一些全局适用的常量等内容
    17 module.exports=hello;
    18 //module.exports=config;
    19  
    20 //方式二
    21 //可以调用多个,用于多个函数或变量的调用
    22 // module.exports={
    23 //     hello,greet,config
    24 // };
     
    main.js
    1 'use strict';
    2 // 引入test模块,此时两个模块在同一目录下,注意写路径。
    3 //方式一
    4 var hello=require('./test');
    5 hello();
    6 // var config = require('./test');
    7 // console.log(config.database,config.username);

    我们来看hello,此时传入的是一个void类型的方法!

    再来看config,此时传入的是一个对象!

    1 //方式二
    2 //var test=require('./test');
    3 //test.hello();
    4 //test.greet('dragon');
    5 //console.log(test.config);               //Object{database:"daname",username:"username",......}

    此时,传入的是一个整体对象(类似java类的一个对象),拥有hello()和greet()两个方法,并且可以调用config常量。

  • 相关阅读:
    ZOJ 3705 Applications
    UVA 220 Othello
    HDU 2084 数塔
    第五章:变量
    第四章:注释
    第三章:程序结构
    第二章:项目的创建和环境熟悉
    第一章:c#开发环境安装
    处理字段串
    查询表的列信息
  • 原文地址:https://www.cnblogs.com/jfl-xx/p/7210734.html
Copyright © 2011-2022 走看看