GithubHelp home page GithubHelp logo

pwfoo / namespace.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alexyork/namespace.js

0.0 2.0 0.0 157 KB

A simple namespace function for JavaScript.

License: Apache License 2.0

JavaScript 100.00%

namespace.js's Introduction

namespace.js

A simple namespace function for JavaScript.

Background

To organise code into logical groups, programming languages such as Java and C# have the concept of a namespace.

JavaScript does not have the concept of namespaces - but similar functionality can be created. Namespace.js does just that.

Why?

In some of JavaScript's module patterns, it is often recommended to organise these modules into some kind of a custom namespace to reduce global clutter:

// Create a namespace called 'Animal'
var Animal = window.Animal || {};

Animal.Cat = {
    speak: function() {
    	return 'Meow!';
    }
};

Animal.Cat.speak(); // "Meow!"

For longer namespaces, the code for creating the custom namespace can quickly become ugly and unwieldy:

// Create a namespace called 'Animal.Mammal.Feline'
var Animal = window.Animal || {};
Animal.Mammal = window.Animal.Mammal || {};
Animal.Mammal.Feline = window.Animal.Mammal.Feline || {};

Animal.Mammal.Feline.Cat = {
    speak: function() {
    	return 'Meow!';
    }
};

Animal.Mammal.Feline.Cat.speak(); // "Meow!"

In the snippet above, the top 3 lines of code are ugly and repetative. Namespace.js aims to clean up the process of defining the custom namespace:

namespace('Animal.Mammal.Feline');

Animal.Mammal.Feline.Cat = {
    speak: function() {
    	return 'Meow!';
    }
};

Animal.Mammal.Feline.Cat.speak(); // "Meow!"

Usage (browser)

  1. Include namespace.js in your HTML:
<script src="namespace.js"></script>
  1. Define and then extend a namespace in your JavaScript:
namespace('Animal.Mammal.Feline');

Animal.Mammal.Feline.Cat = {
    speak: function() {
    	return 'Meow!';
    }
};
  1. Call your namespaced JavaScript code from elsewhere:
Animal.Mammal.Feline.Cat.speak(); // returns 'Meow!'

// or

var cat = Animal.Mammal.Feline.Cat;
cat.speak(); // returns 'Meow!'

Modules? Self-executing functions?

Namespace.js does not care what you do with the namespace. It doesn't matter if you use a fancy JavaScript module pattern, or wrap all or none of your code in a self-executing function.

All namespace.js does is create the namespace in a readable and concise manner.

Usage (Node.js)

Currently, namespace.js will not work in Node because window is referenced in both the source and the spec files. I am sure this is trivial to fix, but I have not gotten round to it yet since I made this and used it within the context of a browser, not a Node app.

namespace.js's People

Contributors

alexyork avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.