Jump to content

Recommended Posts

Hello folks,

I think the question is rather self explicit.  I need to create an object from which I can create instances during the life of my application.  So I opted for a constructor object.  I just don't know how to make it global so that all my scripts can have access to it.  I have tried including it in a namespace without succes

var myNamespace = {

     myConstructorObject:function(){

          //Attributes and methods of my constructor go here

     }

}

Also tried this...


var myNamespace = {

     function myConstructorObject(){

          //Attributes and methods of my constructor go here

     }

}

In both cases, I create a new instance of my constructor like so
var newInstance = myNamespace.myConstructorObject

I also tired creating the constructor object as a global object outside of my namespace like so

myConstructorObject = function(){

     //Attributes and methods


}

All you have to do is define a function, then call it with the new operator.

 


function MyObject(param){
   this.blah = param;
}


var obj = new MyObject('hi');
If you want to create a namespace you can, you'd just declare the function as a property of that object:

var myNamespace = {
    MyObject: function(param){
        this.blah = param;
    }
};

var obj = new myNamespace.MyObject();

Custom Constructors can be created with functions as mentioned above (actually, functions returning this or a property of this can also be used neatly for imitating classes as you know them in Java).

 

For example, assuming that I want to create a global constructor "TypedArray", I'd write something like this...

 

window.TypedArray = function(typeToUse /*, args (as array or array elts) */) {this.type = typeToUse;this.new_array = [];if(arguments.length > 1) {for(var i = 0; i < arguments.length; i++) {if("array" !== typeof arguments[i]) {if(this.type !== typeof arguments[i]) continue;this.new_array[this.new_array.length] = arguments[i];} else {for(var n = 0; n < arguments[i].length; n++) {if(this.type !== typeof arguments[i][n]) continue;this.new_array[this.new_array.length] = arguments[i][n];}}}}return this;};

var x = new TypedArray("string"); // define a new typed array
var a = x.new_array; // a is now the array
var t = x.type; // t now holds the value of type
Hope that helped you a little bit.

 

Edit: Fixed code formatting... made a mistake with closing a comment...

code=auto:0 also messed up spacing... Oh, whatever.

Edited by Irate
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.