zoukankan      html  css  js  c++  java
  • Node简单的模块化小例子

    模块化解决了JavaScript作用域互相污染问题,这里以写一个学校小例子,便于理解node的这个写法和一些简单的API(比如forEach这个.....)。

     文件:

    依次student.js,teacher.js,kclass.js(class为保留字,不能命名),index.js,school.js

    function add(student) {
        console.log('Add student:' + student)
    }
    
    exports.add = add
    function add(teacher) {
        console.log('Add teacher:' + teacher)
    }
    
    exports.add = add
    var student = require('./student')
    var teacher = require('./teacher')
    
    function add(teacherName, students) {
        teacher.add(teacherName)
    
        students.forEach(function(item, index) {
            student.add(item)
        })
    }
    
    exports.add = add
    var kclass = require('./kclass')
    
    exports.add = function(kclasses) {
        kclasses.forEach(function(item,index) {
            var _klass = item
            var teacherName = item.teacherName
            var students = item.students
    
            kclass.add(teacherName, students)
        })
    }
    var school = require('./index.js')
    
    var classesArr = [
        {
            'teacherName':'Bob',
            'students':['SB1','SB2']
        }
    ];
    
    school.add(classesArr);
  • 相关阅读:
    Java文件流应用:复制文件
    Java IO流之文件流
    初识Java-IO流
    Java集合之Properties
    Java之FilenameFilter接口
    Java之File类
    Java内部类
    Java常用类之要点总结
    Java异常类(Throwable)
    php分布式缓存系统 Memcached 入门
  • 原文地址:https://www.cnblogs.com/zhangmingzhao/p/7569582.html
Copyright © 2011-2022 走看看