0

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?

Den
  • 16,686
  • 4
  • 47
  • 87
  • 1
    To some degree. If your `test` npm package exports `someClass`, and you require it as `const test = require('test');` then you have `test.someClass.someFunction`. – Alex Blex Oct 02 '17 at 15:25
  • 1
    There is no such thing as a namespace at the technical level. Of course you can have objects which have objects as properties etc. recursively, which amounts to about the same thing you're describing. Somewhat unclear what exactly you're expecting as answer though. – deceze Oct 02 '17 at 15:26
  • @AlexBlex so in this case, unless I actually have a `const` declaration, there is no concept as `packageName.class.member`? – Den Oct 02 '17 at 15:26
  • @deceze good call, what I am looking for is a formal accepted way to fully address JS API entities from, e.g. `owner`->`child`->`child`, if one exists. – Den Oct 02 '17 at 15:28
  • 1
    Not for npm modules. You need to `require` one to use it. – Alex Blex Oct 02 '17 at 15:30
  • 1
    That's a very malleable thing in Javascript and depends on how the API is written. If you can `require()` it, the top level is arbitrarily named by you. – deceze Oct 02 '17 at 15:30

1 Answers1

1

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.

agm1984
  • 15,500
  • 6
  • 89
  • 113