Calling a JS object class that's in a separate .js file ?

1,113 views
Skip to first unread message

Masoom Tulsiani

unread,
Feb 25, 2015, 5:53:09 AM2/25/15
to meteo...@googlegroups.com
I'm new to meteor. I've been trying to write my meteor code the "object-oriented" way so I'm trying to create different javascript classes in different files and try to create an object and call it's methods from the main app.js.  I have a parsing class called Message.JS with methods and an app.js. 

I would like to instantiate the object and call the methods defined in my Message.JS from my app.js?

I'm aware that Javascript has no include and require. Is there any easy way to include/import JS classes in meteor? 



Masoom Tulsiani

unread,
Feb 25, 2015, 7:24:51 AM2/25/15
to meteo...@googlegroups.com

message.js

  var Message = {


    init: function( message ){

   /* Initialization function with some properties */

   }

  getHash: function() {
    return this.hash_table;
   }

  parseMessage: function(input) {

 /* Some Parsing logic */

  return(hash_table);
 }
 }

app.js

if (Meteor.isServer) {


 var msg = new Message.init(response);
 var hash =  msg.getHash();
 console.log(hash);

}

Rob Fallows

unread,
Feb 25, 2015, 8:27:26 AM2/25/15
to meteo...@googlegroups.com
The use of "var" restricts the scope of Message to the file in which it is defined (basically, each file is automatically wrapped in a function() {...}. You should remove the "var" to globally scope Message.

As an alternative, you can put the file in the "compatibility" folder, which ensures it's not wrapped within a function.

mr zafod

unread,
Feb 25, 2015, 12:08:05 PM2/25/15
to meteo...@googlegroups.com
Hi. Are you shure your logic is correct in 


var msg = new Message.init(response);
var hash =  msg.getHash();


Because msg is instance of Message.init and it doesnt have getHash method

среда, 25 февраля 2015 г., 18:24:51 UTC+6 пользователь Masoom Tulsiani написал:

Masoom Tulsiani

unread,
Feb 25, 2015, 12:13:58 PM2/25/15
to meteo...@googlegroups.com
The full Message.init function

 init: function( message ){

        // Store the message.
        this.message = message;

        // parse the message and init the hash table
        this.hash_table = Message.parseMessage(this.message);

        // init the hash getter
        this.getHash = Message.getHash;

        // init the hash logger
        this.logHash = Message.logHash;

        // Returns this object reference.
        return( this );

    },
Reply all
Reply to author
Forward
0 new messages