0

I am new to Node and JS, trying to implement a module. Inside my module I want to have an object, that I can initialize instances of it in other methods of my module (specifically in my case its a response object). So my code is:

exports.myModule = {

//response object
Response: function()
{
    // some initializing and functions
    function testFunction(){
        console.log("test function inside object was called");
    }
}



//now i am trying to create an instance
tester: function() {
    var res = new Response();
    res.testFunction();
}
}

However I am getting syntax errors that I do not understand (this code doesn't make sense to its purpose, since I am still testing basic creation of the object In my module,

EDITED

Now when creating a new response I get the error: ReferenceError: response is not defined

8
  • Lets get a look at dat error yo! Commented Jan 9, 2017 at 18:05
  • 1
    Primarily because I see syntax issue all over the snippet you provided ^^ Commented Jan 9, 2017 at 18:06
  • Please see edits @TheDembinski, Commented Jan 9, 2017 at 18:18
  • i was about to answer that you were wrong in Response: function() Commented Jan 9, 2017 at 18:22
  • You cant reference Response like. You could do something like: module.exports = {...} and then reference the module. Its very garish in all honesty. Ill provide an answer and see if you like it Commented Jan 9, 2017 at 18:22

3 Answers 3

1

I eventually solved this by taking the object declaration outside of the module like this:

function Response(){
// some initializing and functions
    function testFunction(){
        console.log("test function inside object was called");
    }
}

var Foo = {

//now i am trying to create an instance
tester: function() {
    var res = new Foo.Response();
    res.testFunction();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try something like

var Foo = {

//response object
Response: function()
{
    // some initializing and functions
    function testFunction(){
        console.log("test function inside object was called");
    }
}



//now i am trying to create an instance
tester: function() {
    var res = new Foo.Response();
    res.testFunction();
}
}
module.exports = Foo;

Firstly - there's enormous issues with this. Like the Response method is just weird. But I didnt want to edit your original snippet that much.

And like a mentioned in my comment, there are better ways of doing this, even if you fix the glaring errors I would recommend simply googling around for a Node tutorial at this point.

Edit: If anyone ever finds themselves here, please refer the answer that the OP provided below after doing some general background knowledge work.

A working example might resemble:

module.exports = {

    //response object
    Response: function()
    {
        // some initializing and functions
        this.testFunction = function (){
            console.log("test function inside object was called");
        }
    },


    //now i am trying to create an instance
    tester: function() {
        var res = new this.Response();
        res.testFunction();
    }
};

7 Comments

Thanks,now I get the message: ReferenceError: Foo is not defined I googling for that, just specifically could not find this situation, maybe because I am not doing it in the best way
Make sure you look up all the things! If you're done with this thread, go ahead and delete it or accept the answer.
There's an error; function testFunction(){ is supposed to be this.testFunction = function () {
I accept because your answer is more educational, even though my answer is the way I found more convenient. With time I will understand the correct design patterns for this subject. Thanks for the help!
@ChrisG - Thats definitely a possible take on it. I didn't want/didn't know how to address all the issues with snippet provided. Figured it would be better to gently hint towards the right direction.
|
0

The problem is the context. When you do new Response, it looks for it in global space, where that function is not defined. So, in order to access the function use this.Response as I did, or Foo.Response as did The Dembinski.

module.exports = {

    //response object
    Response: function()
    {
        // some initializing and functions
        this.testFunction = function (){
            console.log("test function inside object was called");
        }
    },


    //now i am trying to create an instance
    tester: function() {
        var res = new this.Response();
        res.testFunction();
    }
};

Comments

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.