-2

This might be basic, but since I am new to this I cant understand if I am doing it wrong- and its a bad practice. Say I have 3 JS files :

  1. a.js
  2. b.js
  3. sdk.js

say that sdk.js is some SDK, and I would like to call functions inside it, from different js files, such as a or b.

Is the way to go is to just call functions in sdk.js from any other file directly ? this seems like no capsulation to me but I couldn't find another way without having object oriented programming.

Curnelious
  • 1
  • 16
  • 76
  • 150
  • 1
    Can't you export from `sdk.js` using es6 export modules ? – Utsav Patel Mar 21 '19 at 08:33
  • 2
    What is your target environment? Browsers or servers? – ethane Mar 21 '19 at 08:34
  • If the functions are exposed globally, you can access them from anywhere. if they are in a module, you need to import it. – VLAZ Mar 21 '19 at 08:34
  • @ethane browser. – Curnelious Mar 21 '19 at 08:35
  • 3
    as long as `sdk.js` is loaded before, say `a.js` tries to call a function declared in it, you should be good ... usually you'd load a dependant `.js` before the `.js` that depends on it (usually) – Jaromanda X Mar 21 '19 at 08:36
  • @JaromandaX thanks, but still I wonder, if a file a.js call a function in another file b.js, that means file "a" is not reusable, and dependent . – Curnelious Mar 21 '19 at 08:37
  • JavaScript code in a browser all runs in the same environment. Globally defined functions are just that; they're not "in a file". If you don't create a local scope yourself, everything is accessible. – melpomene Mar 21 '19 at 08:38
  • if file a requires something in file b, then of course it is dependant on file b ... as for reusable? not even sure what you mean by reusable – Jaromanda X Mar 21 '19 at 08:39
  • 1
    What do you mean by "delegates" and "function pointers" in the context of JavaScript? – melpomene Mar 21 '19 at 08:40
  • I see. So when a programmer build a serious website, and he wants to create a function from an SDK, say Facebook SDK, and for readability he creates a file called facebookSDK.js , that means every other file that call a function that is inside that SDK, is now dependent in it and can not be reused in another project. – Curnelious Mar 21 '19 at 08:40
  • 1
    @Curnelious Innocence doesn't matter. I didn't downvote the question, but votes are supposed to reflect whether the question 1) shows research effort, 2) is clear, and 3) is useful. IMHO your question isn't very clear (especially with that title) and it shows little research effort. – melpomene Mar 21 '19 at 08:48
  • @Curnelious are you asking if it's possible to make a function in another file static as apposed to it being a member of an instance of a class? – Ian Wise Mar 21 '19 at 08:57

2 Answers2

2

There are several ways to call functions in another file.

1) Native Solution

In your HTML file, make sure your <script> tag importing sdk.js is loaded first, like so

<script src="./sdk.js/"></script>
<script src="./a.js/"></script>
<script src="./b.js/"></script>

As long as sdk.js declares the function in the global namespace, any file loaded afterwords should have access to it

2) ES6

If you can use ES6 features (via babel or webpack, for example), then your other scripts can import the file. Take the following example:

sdk.js:

export var foo = function(){
return "foo";
}

a.js

import foo from "./sdk.js";
foo();

3) Node.js way

Node.js supports require, another way of importing files. These files would operate under their own scope, and you would have to use module exports to make a function or variable "public".

** Worth nothing that this only works in node, not the browser, however

4) Require.js way

You can use the require.js library to import other files as well.

Additional Resources

This SO Question has far more in-depth answers than what I've outlined above. Best of luck!

Ian Wise
  • 706
  • 2
  • 10
  • 31
0

Can use

module.exports = { 
       a:()=>{
           console.log("This is 'a' function");
       },
       b:()=>{
           console.log("This is 'b' function");
       }
}

you can call those functions from next file by importing above file by

const file1 = require('that_file_name');
file1.a();
file1.b();

Hope you got the right answer.

  • @IanWise `ReferenceError: module is not defined` – melpomene Mar 21 '19 at 08:45
  • @melpomene that's because this answer only works in node.js, not the browser – Ian Wise Mar 21 '19 at 08:54
  • @IanWise OP was specifically asking about browser environments, so this answer is not useful. – melpomene Mar 21 '19 at 09:04
  • @melpomene Did the OP ever say that this was for a browser environment? I didn't catch that. It's not in the question or comments now? – Ian Wise Mar 21 '19 at 09:06
  • @IanWise From the comments: "*What is your target environment? Browsers or servers?* – ethane" "*@ethane browser.* – Curnelious" – melpomene Mar 21 '19 at 09:07
  • @melpomene My bad, missed that. I would argue that this answer is still valid though. Perhaps just put a disclaimer on it that it'll only work w/ node. – Ian Wise Mar 21 '19 at 09:09