JavaScript 中的 Require() 函数

发布于 2024-12-09 04:18:44 字数 1158 浏览 0 评论 0 原文

当我打开 Chrome 14 的控制台并输入...

require(或 require(),如果这很重要)时,

我得到:ReferenceError

这意味着 JavaScript 默认情况下没有该功能,对吧?至少在网络浏览器上是这样。

我为什么要谈论这个?
我需要 JavaScript 的 Markdown 解析器。

该怎么办?
我像往常一样打开 GitHub 并 搜索它。第一个符合我需求的结果是 this这个

通常(我对 JavaScript 不太擅长)我使用

#1 脚本的用法:

var input = "# Heading\n\nParagraph";
var output = require( "markdown" ).toHTML( input );
print( output );

#2 脚本的用法:

var marked = require('marked');
console.log(marked('i am using __markdown__.'));

require() 在哪里 来自?谢谢您的建议!

When I open console of Chrome 14 and type...

require (or require(), if that matters)

I get: ReferenceError.

This means that JavaScript doesn't have that function by default, right? At least on web browsers.

Why I'm talking about that?
I needed Markdown parser for JavaScript.

What to do?
I, as usually, opened GitHub and searched for it. The first results that matched my needs was this and this.

Usually (I'm not that good with JavaScript) I include script I want to use before my code using <script /> tag and then... well - use it. But this time I don't get what's happening... :(

Usage for #1 script:

var input = "# Heading\n\nParagraph";
var output = require( "markdown" ).toHTML( input );
print( output );

Usage for #2 script:

var marked = require('marked');
console.log(marked('i am using __markdown__.'));

Where does that require() came from? Thanks in an advice! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你又不是我 2024-12-16 04:18:44

这是一种包含 node.js 包的方法。幸运的是,您链接到的第一个包 markdown-js,很聪明。它检查是否包含在节点包中,如果没有,则将 markdown 对象设置为 window.markdown。因此,您所要做的就是将此文件包含在

It's a way to include node.js packages. Luckily, the first package you linked to, markdown-js, is very smart. It checks whether it is included as a node package, and if not, will set the markdown object to window.markdown. So all you have to do is include this file in a <script> tag and you should be able to use the markdown object from the global scope.

葬花如无物 2024-12-16 04:18:44

从您链接到的页面:

将它与 CommonJS 一起使用的简单方法是:

看起来 require 来自 CommonJS

From the page you link to:

The simple way to use it with CommonJS is:

Looks like require comes from CommonJS

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文