0

How to use the function from another js file .

example:

The master.js file contain the following code.

 if(typeof(CONVERTER) == "undefined") var CONVERTER = {};
    (function(_e) {
      "use strict";
      var synmonth = 29.530588853;     //synodic month (new Moon to new Moon)
      var ptsa = new Array(485, 203, 199, 14, 12, 12, 12, 9, 8)
      var ptsb = new Array(324.96, 337.23,320.81, 227.73, 15.45)
      var ptsc = new Array(1934.136, 4777.259, 1222.114, 16859.074)

    function GetAdjusted(yea,mx,dx){
    .......
     return(result);
    }

      _e.GetAdjusted = function(yea,mx,dx) {
        return GetAdjusted(yea,mx,dx);
      };
    }(CONVERTER));

How to use/declare the function in another js file using node.js

5
  • 2
    require the module. nodejs.org/api/modules.html Commented Jul 22, 2015 at 4:27
  • I'm getting an error TypeError: Object #<Object> has no method 'GetAdjusted'. But if I deploy above code in web base is working fine. Commented Jul 22, 2015 at 4:43
  • You are probably not creating the module correctly. Did you follow the examples in the link? Commented Jul 22, 2015 at 4:47
  • The module created by another developer. I'm just a user. But I don't see any "exports" keyword in the master.js, Does it means the code ONLY suitable use in web not for node.js ? Commented Jul 22, 2015 at 4:52
  • Hi; Felix Kling, Thanks. By adding the "module.exports.GetAdjusted=GetAdjusted" solve the problem Commented Jul 22, 2015 at 5:16

1 Answer 1

1

Your code is probably extracted from a Frontend module. Assuming you want to use the getAdjusted function which have CONVERTER as dependency. You can write as following:

"use strict";

module.exports = function(CONVERTER){
  if(typeof(CONVERTER) == "undefined"){
    CONVERTER = {};  
  } 
  var synmonth = 29.530588853;     //synodic month (new Moon to new Moon)
  var ptsa = new Array(485, 203, 199, 14, 12, 12, 12, 9, 8);
  var ptsb = new Array(324.96, 337.23,320.81, 227.73, 15.45);
  var ptsc = new Array(1934.136, 4777.259, 1222.114, 16859.074);

  var getAdjusted = function getAdjusted(yea,mx,dx){
   // your code here
   return(result);
  };

  return getAdjusted;
};

In order to use it from another file

//Assuming your CONVERTER object exists and pass it into require
var getAdjusted = require("/path/to/your/file")(CONVERTER);

// use it. Assuming yea, mx, dx exists.
getAdjusted(yea,mx,dx);
Sign up to request clarification or add additional context in comments.

1 Comment

I don't understand what does the (CONVERTER) mean at the end of the require statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.