Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Javascript Namespace Notation #8

Open
box-lin opened this issue Jun 30, 2023 · 0 comments
Open

Javascript Namespace Notation #8

box-lin opened this issue Jun 30, 2023 · 0 comments

Comments

@box-lin
Copy link
Owner

box-lin commented Jun 30, 2023

The benefit of using namespace notation in JavaScript, often created via objects or immediately-invoked function expressions (IIFE), is primarily for code organization and avoiding name clashes, which become especially important in larger codebases or when using third-party libraries.

Let's consider some of the specific benefits:

  1. Organization: With namespaces, you can group logically related code together. This makes the code easier to understand and maintain.
  2. Avoiding Name Collisions: Without namespaces, if you define a function or a variable that has the same name as a function or variable in another script file, the last script file loaded will overwrite the previous ones. With namespaces, you can avoid such collisions.
  3. Scope Control: Namespaces help in controlling the scope of a variable or function. Only those properties and methods that are attached to the namespace are accessible from the outside. This is a good way to encapsulate your code and control what's publicly accessible, similar to using private and public scopes.
  4. Code Modularity: Namespacing enables better modularity which allows parts of code to be used as modules. These modules can be developed independently and then integrated together with lesser risks of function or variable name conflicts.

In JavaScript, namespaces can be created by simply defining an object:

var MyApp = MyApp || {};

You can then attach properties and methods to this object:

MyApp.myFunction = function() {
    // do something
};
MyApp.myVariable = 42;

The previous one checks for the existence of MyApp in the current scope, if you want explicitly checks for MyApp on the window object, you can do as following:

var MyApp = window.MyApp || {}

Another example

var MyApp = window.MyApp || {};
(function(){
     this.hello = function() {
       console.log("hello");
   }
}).call(MyApp);

A function is created and immediately invoked. The call method is used to invoke this function and set the this context to MyApp. Inside the function, this refers to MyApp because of the call(MyApp) method.

So, this.hello = function() {...} will add a hello method to MyApp. After running this code, you can call MyApp.hello(), and it will log "hello" to the console.

The purpose of this pattern is to organize the code in a way that helps prevent global namespace pollution and keep functions logically grouped together.

@box-lin box-lin added this to the Javascript milestone Jun 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant