4

I have simple module query.js:

module.exports = function(queryString){
     return{
         string: queryString
     };
};

Now I am loading this module from another module and create instance out of it:

var Query = require("./query");
var myQuery = new Query("SELECT * FROM `mytabel`");

console.log(myQuery instanceof Query); // Ouputs false
console.log(myQuery.constructor == Query); // Outputs false

As I understood from the nodejs documentation, require("Query") gives me module.exports object which in my case is anonymous function that accepts 1 parameter queryString. I use this function to create new object myQuery and yet, it is not instance of Query.

MY QUESTION: How can I check if myQuery is created from the Query function and why are both outputs false when they should be true in my opinion?

2
  • What does console.log(require("Query")); give you? Don't you need to use require("query"); (lowercase)? Commented Nov 6, 2013 at 13:59
  • @WillemMulder Yes sorry, I wrote this minified example from scratch, It is actutally in small letters. Fixed it. Commented Nov 6, 2013 at 14:01

2 Answers 2

5

While dskh is correct, a little more explanation might be valuable.

A function can only act as a constructor when it returns something else than an Object.

Once a function returns an Object (e.g. function SomeFunc() { return {}; }), if you call var a = new SomeFunc();, variable a will become the returned Object, which is a plain Object, and will not be a created instance of SomeFunc. If SomeFunc returned something else than an Object, the JS engine would ignore whatever the function returns and instead return an Object of instance SomeFunc, which is what you want.

So in your case, you return an Object, but in order to make it work as a constructor you need to return nothing (or at least anything else than an Object):

module.exports = function(queryString){
     this.string = queryString;
};

See, it does not return an Object, so you will get an instance of Query everytime you call new Query().

Sign up to request clarification or add additional context in comments.

Comments

3

Right now you're exporting an object. You need to export a class.

function Query(querystring){
    this.querystring = querystring;
}

module.exports = Query;

3 Comments

You mean I am exporting a class but the class returns an object which is assigned to myQuery and it is no longer instance of Query? Silly me, how did I miss that :).
Node's module system doesn't type things. When you require something, it just returns the module.exports variable from that file. In your code, the module.exports returns an unnamed function, which is an instance of function. In my answer I return a named function with a capital letter (this is as close to a class as you will get in javascript). You can then use this in an instanceof statement. Also, your require('query') statement probably won't work right now because it will look in your package.json for a module called query. It should be a file path e.g. require('./query')
Yes that is what I meant and yes, you are correct about the last part - the code above was only to illustrate a point not actual 100% working script.

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.