1

I don't know how to explain the issue I am facing. I had made a small snippet. Please check below:

var Package = {};

Object.defineProperty(Object.prototype, 'inherit',
    {
        value: function(Parent, args)
        {
            var temp = function(){};
            temp.prototype = Parent.prototype;
            this.prototype = new temp();
            this.uber = Parent.prototype;
            this.prototype.constructor = this;
        },
        enumerable: false
    });

var Module1 = Package.Module1 = function() {
  // code;
};
Module1.prototype.method1 = function() {
};


var Module2 = Package.Module2 = function() {
  // code;
};
Module2.prototype.method2 = function() {
};


var Module3 = Package.Module3 = function() {
  // code;
};
// using custom : object.prototype.inherit
// Module3 inherit Module1 and 2;
Module3.inherit(Module1);
Module3.inherit(Module2);
Module3.prototype.method3 = function() {
};

//creating object 
var mod = new Package.Module3();
mod.method1();
mod.method2();
mod.method3();

Creating the mod object I can access the method1,2 and 3. But actually I want to call the methods without creating the object, like Package.Module3.method1(); how is it possible?

4
  • 1
    Does Package.Module3 have to be a constructor function? You could just create it as an object and assign the functions to it. Or you assign an instance of Module3 to Package.Module3. It all depends on what else you want to do with it. Commented Jun 19, 2012 at 16:46
  • right now i have. but it can be avoided. Commented Jun 19, 2012 at 16:47
  • Singleton can be object literals in javascript. You don't have great an instance of the variable using new. More information here: blog.anselmbradford.com/2009/04/21/… Commented Jun 19, 2012 at 16:49
  • its not possible to add methods using prototype if we use.... : var method = new function() {}; Commented Jun 19, 2012 at 17:11

2 Answers 2

5

Object literal notation?

var Package = {
    ModuleA : {
        methodA : function(){},
        methodB : function(){}
    },
    ModuleB : {
        methodC : function(){},
        methodD : function(){}
    }
}

Package.ModuleA.methodA();
Sign up to request clarification or add additional context in comments.

2 Comments

but my inherit method wont work. the snippet iam using function inherit(C, P) { var F = function(){}; F.prototype = P.prototype; C.prototype = new F(); C.uber = P.prototype; C.prototype.constructor = C; }
@JaisonJustus then create an external inherit function instead: method : ns.inheritFrom(someclass,someclass2...)
1

Try something like this:

var Module3 = Package.Module3 = (function() {       
   var Module3 = function(){
       // using custom : object.prototype.inherit
       // Module3 inherit Module1 and 2;
   };
   Module3.inherit(Module1);
   Module3.inherit(Module2);
   Module3.prototype.method3 = function() {
   };
   return new Module3();
})();

In this case you have just one instance of Module3 class.

Usage:

Package.Module3.method1();
Package.Module3.method2();
Package.Module3.method3();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.