DEV Community

Lukas Gaucas
Lukas Gaucas

Posted on • Updated 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)

The JavaScript Brief

1. Top 5 MERN STACK projects to improve your practical understanding

Boost your MERN Stack development skills by undertaking interesting beginner projects. These five engaging projects cover web applications and range from social media website applications to geo-social networking maps. Hone your understanding and apply modern techniques backed up by hands-on experience.

2. How To Optimize Your React App’s Performance

Learn the best optimizing techniques to make your React applications faster and more efficient. Focusing on the identification of performance bottlenecks and common pitfalls to avoid, these optimization strategies will keep your applications running smoothly even when faced with growing complexity.

3. A story of let, const, object mutation, and a bug in my code

In the pursuit of bug-free code, explore an incident involving a mix-up between const and let, making sure your custom code works effectively with third

party documentation. Discover best practices on program flow and learn about JavaScript's unpredictable aspects to ensure your core code is robust.