I'm creating a Node.js application. I have a project with this structure:
[Project Folder]
|
|---[plc]
| |--- plc.js
| |--- scheduler.js
|
|---[source]
| |--- source.js
|
|---[test]
|--- test.js
The files plc.js , scheduler.js and source.js are "objects", they require other objects and, at the end of file, have an "export" of the object. In particular the file plc.js has an weird behavior. First the code:
var mod_pollist = require('./polling_list.js'); // Polling list.
var mod_operation = require('./operation.js'); // Single operation.
var mod_scheduler = require('./scheduler.js'); // Scheduler object.
var mod_events = require('events'); // Event emitter
var mod_util = require('../util.js'); // Util functions
function plc(comm_driver){
var self = this;
// Other variables are set here
}
// Other functions written as plc.prototype.something = function(parameters){...}
module.exports = plc;
Now the weird behavior: all the other files have at the top of the file the code for importing the plc.js ( var mod_plc = require('../plc/plc.js');
or var mod_plc = require('./plc.js');
for the scheduler) but only in test.js it works correctly, infact if I write
if(PLC instanceof mod_plc)
console.log('yes');
in the file test.js I can find a 'yes' on the console, if I write the same code in the other files I obtain an error:
if(PLC instanceof mod_plc)
^
TypeError: Expecting a function in instanceof check, but got #<Object>
at Object.<anonymous> (C:\Users\Massimo\workspace\Scada\plc\scheduler.js:16:
19)
A "temporary solution" could be
if(PLC instanceof mod_plc.constructor)
console.log('yes');
but I don't think is the real solution because with all other objects (I've more than 20 files written like plc.js ) this problem doesn't exist.
Any suggestion? Do you need more informations? Thanks
To summarise my comments from above:
Given that:
TypeError
tells you mod_plc
is an Object
(not a constructor function); and mod_plc.constructor
gives you the expected behaviour; it looks like your PLC
variable has somewhere been assigned an instance of mod_plc
(hence is no longer a reference to the expected constructor function).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.