如何从module.exports访问函数的属性

时间:2018-01-08 20:36:56

标签: javascript webpack

我的index.js

中有以下代码
const test = require('./js/lerifier/start');
const $ = require("jquery");

$( document ).ready(function() {
  console.log( "ready!" );
  console.log(test.init());
});

我想从this.init()

上的lerifier函数访问start.js函数
const $ = require("jquery");
const SVGInjector = require('svg-injector');



module.exports = function Verifier() {


  this.init = function () {

    let mySVGsToInject = $('img.inject-me');

    let injectorOptions = {
        evalScripts: 'once',
        pngFallback: 'assets/png',
        each: function (svg) {
            // Callback after each SVG is injected
            console.log('SVG injected: ' + svg.getAttribute('id'));
        }
    };

    // Trigger the injection
    SVGInjector(mySVGsToInject, injectorOptions, function (totalSVGsInjected) {
        // Callback after all SVGs are injected
        console.log('We injected ' + totalSVGsInjected + ' SVG(s)!');
    });
  }
};

使用index.js中所述的代码,我得到的错误消息是 test.init is not a function,我知道我做错了什么或误解了module.exports的使用。

1 个答案:

答案 0 :(得分:-1)

这样的事情怎么样?在这种情况下,直接引用start.js

中的函数
 const test = require('./js/lerifier/start.js').Verifier;