In .NET we have the concept of a fully-qualified type name. For example, we have System.String
, where System
is the namespace, and String
is the type. Subsequently, we can have member FQNs, e.g. System.String.Substring
, where Substring
is a member.
Does this concept apply to JavaScript-based tooling as well? For example, if I have a test
npm package, is there anything like test.someClass.someFunction
that is considered to be valid per-standard FQN?
JavaScript has a loose type system, so it basically doesn't care about what the type is unless you tell it to. You can change the type on demand as well.
let sample = 5
console.log(typeof sample)
sample = 'ok'
console.log(typeof sample)
node.js uses modules, which means every file is basically a namespace that exports a singleton, starting from your root file.
Where it gets wildly different, is you can set the value of a variable to a function, and it's important to note that its not passing around a copy of this function. It's always using a live reference to the original, which is the basis of closure
, where if you call a variable from inside a function.
If a function has access to the scope of a variable, then it uses it. Closure is a difficult one to grasp, so you should watch some videos on it.
const string = function() { return 'hello' };
Then, you can just run it on demand, or pass a reference to it:
console.log(string)
console.log(string())
const test = string
console.log(test())
You can replicate System.string by going into your root file, and typing const System = {}
to create an object, then you can do System.string = 'hello world'
.
In node, you can create another file called System.js and put module.exports = { string: 'hello' }
and then require it into your root file. The difference in that case is you can call it whatever you want in the root file.