您如何将带有需求和模块导出的npm软件包用作纯JS库

我不确定在这里甚至没有问正确的问题,对不起,但我认为这两个一般性问题是:

  • In what way do you need to modify a node.js package using require etc to be used as a plain embedded script/library in HTML?
  • How do you call a class constructor (?) in JS as a function to validate a form field?

I'm trying to use this small JS library NoSwearingPlease (which is an npm package) in an environment with no node or build system – so I'm just trying to call it like you would jQuery or something with a script & src in the HTML, and then utilise it with a small inline script.

我可以看到要使此工作正常,需要做一些事情:

  1. JSON文件需要以其他方式调用(不使用require等)
  2. checker变量需要重写,再次不需要

我尝试使用jQuery getJSON,但我只是不了解该库的类和作用域位,不足以使用它,我认为:

var noswearlist = $.getJSON( "./noswearing-swears.json" );
function() {
    console.log( "got swear list from inline script" );
  })
    .fail(function() {
      console.log( "failed to get swear list" );
    })
  noswearlist.done(function() {
    console.log( "done callback as child of noswearlist variable" );
    var checker = new NoSwearing(noswearlist);
    console.log(checker);
});

请暂停。谢谢!