什么时候应该使用require()和什么时候使用define()? [英] When should I use require() and when to use define()?

查看:322
本文介绍了什么时候应该使用require()和什么时候使用define()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近几天,我一直在玩requirejs.我试图理解定义和需求之间的区别.

I have being playing around with requirejs for the last few days. I am trying to understand the differences between define and require.

Define似乎允许模块分离,并允许遵守依存关系顺序.但是它将下载开始所需的所有文件.虽然仅在需要时加载需要的东西.

Define seems to allow for module separation and allow for dependency ordering to be adhere. But it downloads all the files it needs to begin with. Whilst require only loads what you need when you need it.

这两个可以一起使用吗?应将它们分别用于什么目的?

Can these two be used together and for what purposes should each of them be used?

推荐答案

使用define,您可以在require.js中注册一个模块,然后您可以在其他模块定义或require语句中依赖该模块. 使用require,您可以仅"加载/使用可以由require.js加载的模块或javascript文件. 例如,请查看文档

With define you register a module in require.js that you can then depend on in other module definitions or require statements. With require you "just" load/use a module or javascript file that can be loaded by require.js. For examples have a look at the documentation

我的经验法则:

  • 定义:如果要声明模块,则应用程序的其他部分将依赖于此.

  • Define: If you want to declare a module other parts of your application will depend on.

要求:如果您只是想加载和使用东西.

Require: If you just want to load and use stuff.

这篇关于什么时候应该使用require()和什么时候使用define()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆