Search code examples
javascriptfunctionobjectnestedobject-literal

Nested functions within an object javascript


I am trying to add an additional function to my object. I am getting an error of undefined. I am confused. I want to be able to enter this line and have the object function execute.

var Button = System.Windows.Forms.Control.TabControl();
Button.TabPages.Add();

This is the code I have so far:

 System.Windows = {};
 System.Windows.Forms={};
 System.Windows.Forms.Control={
    TabControl: function () {
         this.Node;
         var self=this;
         this._ID="id test";
         TabPages= {
             //
             Add: function () {
                 Debug.WriteLine("ADD ID:"+self._ID);
             }
         }
         this.Focus = function () {
             this.Node.focus();
         }
         this.NodeCreate = function () {
             Debug.WriteLine("node created");
             var btn = document.createElement("button");
             btn.type = "button";
             btn.name = this._Name;
             this.Node = btn;
         }
         this.NodeCreate();
     }
 }

Somehow I think I am confusing the scopes or something. Any help would be appreciated. Thanks.


Solution

  • This runs without errors, but doesn't physically add the button to the page because you never added it to the document. I assume that's the next step.

        var System = {
                Windows : {
                    Forms : {
                        Control : {
                            TabControl : function ()
                            {
                                var self=this;
                                this.Node = null;
                                this._ID="id test";
                                this.TabPages = {
                                    //
                                    Add: function () {
                                        console.log("ADD ID:"+self._ID);
                                    }
                                };
                                this.Focus = function () {
                                     this.Node.focus();
                                };
                                this.NodeCreate = function () {
                                    console.log("node created");
                                    var btn = document.createElement("button");
                                    btn.type = "button";
                                    btn.name = this._Name;
                                    self.Node = btn;
                                    return self.Node;
                                }
                                self.NodeCreate();
                                return this;
                            }
                        }
                    }
                }
            },
            Button = new System.Windows.Forms.Control.TabControl();
    
        Button.TabPages.Add();