DEV Community

Lukas Gaucas
Lukas Gaucas

Posted on • Edited on

Prototypeless namespaces examples in JS | 2 ed.

Revising prototype-based inheritance in JS discovered a worth mentioned way of defining , some may say "overhead", prototypeless namespaces in global & local scopes , i.e.:

// global namespace :
global_namespace = Object.create(null)
global_namespace['namespace_name'] = "global_namespace"; 
console.log(global_namespace) // {namespace_name: 'global_namespace'}

// local namespace :
(function (){
    // TIP # classically we could use function-scoped var instead of let, but ***"let"*** it be :
    let local_namespace = Object.create(null);
    local_namespace['namespace_name'] = "local_namespace"
    return [local_namespace, global_namespace];
}())
/** Console output :
(2) [{…}, {…}]
0: {namespace_name: 'local_namespace'}
1: {namespace_name: 'global_namespace'} # NOTE : global accessible through n-th tuple nesting
length: 2
[[Prototype]]: Array(0)
*/

local_namespace // local_namespace is not defined at <anonymous> # just as expected
Enter fullscreen mode Exit fullscreen mode

Since ed. 2 :

Prototypeless also means constructorless a.k.a singleton, but not vice versa i.e. constructorless can be prototype-based in JS land :


Related articles

Top comments (0)