drayarms Posted August 9, 2013 Share Posted August 9, 2013 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 sovar newInstance = myNamespace.myConstructorObjectI also tired creating the constructor object as a global object outside of my namespace like so myConstructorObject = function(){ //Attributes and methods } Quote Link to comment https://forums.phpfreaks.com/topic/281010-how-do-you-create-a-global-constructor-object-in-javascript/ Share on other sites More sharing options...
kicken Posted August 9, 2013 Share Posted August 9, 2013 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(); Quote Link to comment https://forums.phpfreaks.com/topic/281010-how-do-you-create-a-global-constructor-object-in-javascript/#findComment-1444262 Share on other sites More sharing options...
Irate Posted August 10, 2013 Share Posted August 10, 2013 (edited) 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 typeHope 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 August 10, 2013 by Irate Quote Link to comment https://forums.phpfreaks.com/topic/281010-how-do-you-create-a-global-constructor-object-in-javascript/#findComment-1444336 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.