Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I declare a namespace in JavaScript?

How do I create a namespace in JavaScript so that my objects and functions aren't overwritten by other same-named objects and functions? I've used the following:

if (Foo == null || typeof(Foo) != "object") { var Foo = new Object();} 

Is there a more elegant or succinct way of doing this?

like image 743
Scott McKenzie Avatar asked May 19 '09 08:05

Scott McKenzie


People also ask

What is namespace in JavaScript with example?

Namespace refers to the programming paradigm of providing scope to the identifiers (names of types, functions, variables, etc) to prevent collisions between them. For instance, the same variable name might be required in a program in different contexts.

What's a namespace in JS?

JavaScript “namespace” is a programming paradigm that is utilized for assigning scope to the identifiers such as variables and function names. It is used to prevent collisions between the same-named variables and functions.

What is a namespace example?

Prominent examples for namespaces include file systems, which assign names to files. Some programming languages organize their variables and subroutines in namespaces. Computer networks and distributed systems assign names to resources, such as computers, printers, websites, and remote files.

How do you define a namespace in TypeScript?

The namespace is used for logical grouping of functionalities. A namespace can include interfaces, classes, functions and variables to support a single or a group of related functionalities. A namespace can be created using the namespace keyword followed by the namespace name.


2 Answers

I use the approach found on the Enterprise jQuery site:

Here is their example showing how to declare private & public properties and functions. Everything is done as a self-executing anonymous function.

(function( skillet, $, undefined ) {     //Private Property     var isHot = true;      //Public Property     skillet.ingredient = "Bacon Strips";      //Public Method     skillet.fry = function() {         var oliveOil;          addItem( "\t\n Butter \n\t" );         addItem( oliveOil );         console.log( "Frying " + skillet.ingredient );     };      //Private Method     function addItem( item ) {         if ( item !== undefined ) {             console.log( "Adding " + $.trim(item) );         }     } }( window.skillet = window.skillet || {}, jQuery )); 

So if you want to access one of the public members you would just go skillet.fry() or skillet.ingredients.

What's really cool is that you can now extend the namespace using the exact same syntax.

//Adding new Functionality to the skillet (function( skillet, $, undefined ) {     //Private Property     var amountOfGrease = "1 Cup";      //Public Method     skillet.toString = function() {         console.log( skillet.quantity + " " +                      skillet.ingredient + " & " +                      amountOfGrease + " of Grease" );         console.log( isHot ? "Hot" : "Cold" );     }; }( window.skillet = window.skillet || {}, jQuery )); 

The third undefined argument

The third, undefined argument is the source of the variable of value undefined. I'm not sure if it's still relevant today, but while working with older browsers / JavaScript standards (ecmascript 5, javascript < 1.8.5 ~ firefox 4), the global-scope variable undefined is writable, so anyone could rewrite its value. The third argument (when not passed a value) creates a variable named undefined which is scoped to the namespace/function. Because no value was passed when you created the name space, it defaults to the value undefined.

like image 85
Jaco Pretorius Avatar answered Oct 13 '22 23:10

Jaco Pretorius


I like this:

var yourNamespace = {      foo: function() {     },      bar: function() {     } };  ...  yourNamespace.foo(); 
like image 36
dfa Avatar answered Oct 14 '22 00:10

dfa