javascript - module.exports和exports.module的区别
PHPz
PHPz 2017-04-10 16:37:16
[JavaScript讨论组]

es6-module.js

class People{
  constructor(name){
    this.name = name;
  }
  sayhi(){
    console.log(`hi ${this.name} !`);
  }
}

exports.module = People;
// module.exports = People;

entry.js

let People = require('./js/es6-module');

let p = new People("Yika");
console.log(p.name);
p.sayHi();

报错:Uncaught TypeError: People is not a function
目录结构

=========webpack之后转为ES5之后是下面这样的==================

function(module, exports) {

    "use strict";

    var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

    function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

    var People = (function () {
      function People(name) {
        _classCallCheck(this, People);

        this.name = name;
      }

      _createClass(People, [{
        key: "sayhi",
        value: function sayhi() {
          console.log("hi " + this.name + " !");
        }
      }]);

      return People;
    })();

    //exports.module = People;

    module.exports = People;

/***/ }
PHPz
PHPz

学习是最好的投资!

全部回复(1)
高洛峰

https://github.com/zwhu/blog/issues/17

刚好上几天写了这个博客,对你有帮助的话,给我点个star吧。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号