Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Introduction to Object-Oriented JavaScript and namespaces in javascript

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 122
    Comment on it

    There are n number of OOPS functionality available in Javascript. Some of the basic OOPS terminologies are namespaces, classes, object, constructor, method and much more. Now I will explain one by one with syntax and definition of these terms.

    Namespaces: Namespace is a container which provides developer bunch of functionality in a unique application with a specific name.

     

    // global namespace
    var MYAPP = MYAPP || {};

     

    The basic terminology definition of Object-Oriented JavaScript are :

    Classes: It is used to define object characteristics.

    Property:An object characteristic, such as color.

    Method:An object capability.

    Constructor: It has the same name as the class containing it. A constructor is called when the instance of an object is created.

    Object: An instance of a class is called object.

     

    Now By giving an example I will explain all the points.

     

    var MYAPP = MYAPP || {};
    
    MYAPP.event = {};
    
    // Create container called MYAPP.commonMethod for common method and properties
    MYAPP.commonMethod = {
      regExForName: "", // define regex for name validation
      regExForPhone: "", // define regex for phone no validation
      validateName: function(name){
        // Do something with name, you can access regExForName variable
        // using "this.regExForName"
      },
     
      validatePhoneNo: function(phoneNo){
        // do something with phone number
      }
    }
    
    // Object together with the method declarations
    MYAPP.event = {
        addListener: function(el, type, fn) {
        // code stuff
        },
        removeListener: function(el, type, fn) {
        // code stuff
        },
        getEvent: function(e) {
        // code stuff
        }
      
        // Can add another method and properties
    }
    
    // Syntax for Using addListener method:
    MYAPP.event.addListener("yourel", "type", callback);

     

    Here In the above example, we can see we have created the namespace MYAPP then we have created a container called MYAPP.commonmethod for common method and property. Then we have define variable for checking validation and then define a function to use all the method.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: