My super-long file (main.js) works fine as is. But I want to split out the functions dealing with 'y' into a separate file for organization. In PHP I would use require('yfunctions.php') and be done with it.
Is there an equivalent in javascript that doesn't require rewriting the function calls?
main.js:
// do stuff
function first(x){
// do stuff with x
}
function second(y){
// do stuff to y
// return y
}
function third(y){
// do stuff with y
}
ultimately bees:
main.js:
require('yfunctions.js');
// do stuff
function first(x){
// do stuff with x
}
yfunctions.js:
function second(y){
// do stuff to y
// return y
}
function third(y){
// do stuff with y
}
The above does not work (it seems). Do I have to add an "exports" declaration to each function in yfunctions.js? Is there not a way to say "export every function in this file as a function?"
(Note, I'm working with node.js / electron ... but I'm curious for general knowledge about how javascript works.)
My super-long file (main.js) works fine as is. But I want to split out the functions dealing with 'y' into a separate file for organization. In PHP I would use require('yfunctions.php') and be done with it.
Is there an equivalent in javascript that doesn't require rewriting the function calls?
main.js:
// do stuff
function first(x){
// do stuff with x
}
function second(y){
// do stuff to y
// return y
}
function third(y){
// do stuff with y
}
ultimately bees:
main.js:
require('yfunctions.js');
// do stuff
function first(x){
// do stuff with x
}
yfunctions.js:
function second(y){
// do stuff to y
// return y
}
function third(y){
// do stuff with y
}
The above does not work (it seems). Do I have to add an "exports" declaration to each function in yfunctions.js? Is there not a way to say "export every function in this file as a function?"
(Note, I'm working with node.js / electron ... but I'm curious for general knowledge about how javascript works.)
Share Improve this question edited Apr 12, 2017 at 12:35 Sayuri Mizuguchi 5,3303 gold badges29 silver badges57 bronze badges asked Apr 12, 2017 at 12:23 Trees4theForestTrees4theForest 1,3964 gold badges22 silver badges53 bronze badges2 Answers
Reset to default 5Use module.exports
to export members of a module. In your example:
module.exports.second = second;
module.exports.third = third;
function second(y){
// do stuff to y
// return y
}
function third(y){
// do stuff with y
}
There's no option to automatically export all members of a module.
If you're working in ES6, the above could be simplified to:
module.exports = {
second,
third
};
function second(y){
// do stuff to y
// return y
}
function third(y){
// do stuff with y
}
Lastly, in your main.js
you can call the exported functions of other modules by assigning a name to the require
statement:
const yfunctions = require('./yfunctions');
yfunctions.second(y);
In this case you have to use module exports, and use require to exports the functions in other archive. And after you can use, check my example
functions.js
module.exports = {
foo: function () {
// do something
},
bar: function () {
// do something
}
};
var tryit = function () {
}
Use functions from functions.js
var callFunction = require('./functions');
console.log(typeof callFunction .foo); // => 'function'
console.log(typeof callFunction .bar); // => 'function'
console.log(typeof callFunction .tryit); // => undefined because does not use exports
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745271733a4619789.html
评论列表(0条)