如何将可重复使用的JavaScript代码移动到单独的文件中,而不是主文件并使用几次?
我有一个具有许多属性的数据表。每次我想使用它时,我都必须复制它们。这使我的视图在巨大的混乱背后。因此,我决定创建一个单独的.js文件并在此处复制可重复使用的代码,但我无法在主文件中引用它。我的.js代码的某些部分就像Bellow:
function loadDataTable() {
$(".yadcf-filter-range-date").persianDatepicker();
dataTable = $('#SP1').DataTable({
serverSide: true,
processing: true,
pageLength: 10,
infoFiltered: true,
orderMulti: false,
scrollX: true,
scrollY: true,
bStateSave: true,
columnDefs: [{ orderable: false, targets: [15] }],
.
.
.
}
我在其他JS文件中创建一个函数:例如Bellow:
function CommonPart(i) {
serverSide: true,
processing: true,
pageLength: 10,
infoFiltered: true,
orderMulti: false,
scrollX: true,
scrollY: true,
bStateSave: true,
columnDefs: [{ orderable: false, targets: [i] }]
}
我不知道如何在主代码中使用Common Part,例如Bellow:
function loadDataTable() {
$(".yadcf-filter-range-date").persianDatepicker();
dataTable = $('#SP1').DataTable({
CommonPart(15);
...
});
I have a DataTable with a lot of properties. Every time I want to use it, I have to copy all of them. It makes my view's code behind a huge mess. So I decide to create a separate .js file and copy the reusable code there but I can not reference it in my main file. Some part of my .js code is like bellow:
function loadDataTable() {
$(".yadcf-filter-range-date").persianDatepicker();
dataTable = $('#SP1').DataTable({
serverSide: true,
processing: true,
pageLength: 10,
infoFiltered: true,
orderMulti: false,
scrollX: true,
scrollY: true,
bStateSave: true,
columnDefs: [{ orderable: false, targets: [15] }],
.
.
.
}
I create a function in another js file for example like bellow:
function CommonPart(i) {
serverSide: true,
processing: true,
pageLength: 10,
infoFiltered: true,
orderMulti: false,
scrollX: true,
scrollY: true,
bStateSave: true,
columnDefs: [{ orderable: false, targets: [i] }]
}
I do not know how to use CommonPart in my main code for example like bellow:
function loadDataTable() {
$(".yadcf-filter-range-date").persianDatepicker();
dataTable = $('#SP1').DataTable({
CommonPart(15);
...
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我首选的方法是用 ecmascript模块 。
您可以将每个功能都放入单独的文件中。您可以将文件命名为您喜欢的每个文件,但是我通常在包含的函数之后将它们命名。对于Web,我将给文件一个
.js
文件扩展名。在节点中,我会给它一个.mjs
扩展名。在每个文件中,prepend export export 这样的功能:
然后从主代码文件中,您可以 import> import 您的函数您从这些独立文件中的函数
之后,您可以将函数像在主代码文件中声明一样(因为现在已导入)。
My preferred way to accomplish this is with ECMAscript modules.
You can place each of those functions into a separate file. You can name the files what every you like, but I usually name them after the function it contains. For web, I'd give the file a
.js
file extension. In node, I'd give it a.mjs
extension.In each file, prepend export on each function like this:
Then from your main code file, you can import your functions from those separate files like this:
After this, you can use the functions as if they were declared in your main code file (because they are now imported).
最好,最受信任的资源ID MDN WebDocs
链接: https:// https://developer.mozilla.org/en-en--en--en-------- US/DOCS/WEB/JAVASCRIPT/GUIDE/模块
如果您仍然感到困惑随时提出问题。
The best and most trusted resource id MDN webdocs
link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
if you are still confused feel free to ask question..