I'm reading this code snippet from here node-printer :
var printer_helper = {},
fs = require("fs"),
child_process = require("child_process"),
os = require("os"),
path = require("path"),
native_lib_path = path.join(__dirname, '../build/Release/node_printer.node'),
printer_helper;
At first glance it seems to me that a variable is being created (or is it an object...) called printer_helper
. I assume that inside this you have the variables child_process
, os
and path
that they refer to their respective modules and are imported with require
.
native_lib_path
apparently it saves the path where the module is locatednode_printer_node
I don't know if what I assumed above is totally correct and I also have the following doubts:
What is the first key for?
Why is the same name of the variable added at the end?
when doing
You declare that printer_helper must contain a new empty object.
Then there is a comma ',' which indicates that you are declaring another variable.
The following variables are not part of
printer_helper
, they are independent. Remember the comma.Finally, re-declaring
printer_helper
has no effect. Well, JavaScript always moves all variable declarations to the beginning of the function and unifies them if they are duplicated, automatically and silently.With respect to
Correct
Correct.
printer_helper
is a variable that contains an empty object.Yes and no.
fs
,child_process
,os
andpath
are variables that are initialized by importing the modules withrequire
. But those variables are NOT inside the object in the variableprinter_helper
, they are independent (note how they are separated by a comma).Correct.
The first key is used to indicate that it
printer_helper
is an object.This seems like a bug almost, because it's unnecessary. Since no value is assigned to it, it just stays the same as it was before (
{}
) and doesn't affect anything. Also, if you continue to look at the code you linked, you can see that both initializations are almost useless, because the following lines initializeprinter_helper
to a new value: