module.exports和exports.module的区别

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;

/***/ }
阅读 5.3k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题