He$h Posted January 20, 2018 Posted January 20, 2018 hey everyone , so i've been learning about javascripts and .js files and i've got a problem , i googled my problem but answers didn't help me in rage MP scripts, here's the problem : im declaring some functions in "functions.js" file , so i can use them in another .js file. but i get this error : function is not defined . for example : i declared function 'myfunc' in functions.js in this way : function myfunc(a,b){ return a*b; } but in the other file when i say for example z=myfunc(5,8) and i expect z to be 40 , but instead i get this error in console : "myfunc is not defined." i even required "functions.js" before the other .js file : my index.js file is like this : require('./functions.js') require('./commands.js') but the functions are still undefined .. Can anyone help me out ??!
He$h Posted January 20, 2018 Author Posted January 20, 2018 got the answer finally so you to do this for example : inside "functions.js" --> function myfunc(a,b){ return a*b; } module.exports = { myfunc : myfunc, } inside the other .js file --> tempJS = require('"path to functions.js"/functions.js'); FUNC = tempJS.myfunc; now FUNC is the same as myfunc .. but still , if you guys have a better idea of how to do it , share it plZ .. 1
rt-2 Posted January 30, 2018 Posted January 30, 2018 (edited) Well, module.exports = { myfunc: function (a,b){ return a*b; }, otherfunc: function (a,b){ return a/b; }, } let Functions = require('"path to functions.js"/functions.js'); Object.assign(global, Functions); // myfunc() and otherfunc() are simply added to the global object and can be used everywhere // you can also use: global.Functions = require('"path to functions.js"/functions.js'); // Functions.myfunc() and Functions.otherfunc() are added to the global object and can be used everywhere Edited January 30, 2018 by rt-2 1
Flow Posted March 1, 2018 Posted March 1, 2018 (edited) @rt-2 feeling really dump of asking that much but i don't get my error. I have a folder called functions. Within are folders for player, vehicles, etc to have a real clean structure. In serverside package index.js i load the function file which is definded (for an example) like this file: index.js in functions folder: let a = require('./player/playerLogin.js'); let b = require('./player/playerLoadFromDB.js'); let c = require('./player/playerGiveWeapon.js'); Object.assign(global, a); Object.assign(global, b); Object.assign(global, c); file playerGiveWeapon.js var DB = require('../../modules/db'); var Players = require('../../modules/players'); module.exports = { pxkPlayerGiveWeapon: function (player, weapon, ammo){ return "start loading from db"; }, test: function (player){ console.log("start loading from db"); } } None of the following work (put in index.js of server package and above the index.js of functions is loaded) let test = test(); test(); Functions.test(); it says that "test" is not defined. What am i doing wrong? Edited March 1, 2018 by cmdflow
Flow Posted March 1, 2018 Posted March 1, 2018 Okay it seems you can't do multible global.Functions = require(fileA) global.Functions = require(fileB) global.Functions = require(fileC) 1
rt-2 Posted March 30, 2018 Posted March 30, 2018 (edited) On 3/1/2018 at 7:18 AM, cmdflow said: Okay it seems you can't do multible global.Functions = require(fileA) global.Functions = require(fileB) global.Functions = require(fileC) should be global.Functions = require(fileA); let b = require(fileB); Object.assign(global.Functions, b); let c = require(fileC); Object.assign(global.Functions, c); What you do will overwrite the whole object everytime. rt-2 Edited March 30, 2018 by rt-2
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now