How to run a nested javascript function?

I am new to object orientated programming in javascript and am trying to understand some functions in a project I am working on.

How would I call/run the internal function (the one listed 'this.getFieldset = function() {') to execute?

function Fieldset() {

    this.id = "";
    this.content = document.createElement("DIV");
    this.content.id = "content";
    this.title = "Title";

    this.getFieldset = function() {

        var div = document.createElement("DIV");
        div.id = this.id;
        var span = document.createElement("SPAN");
        var fieldset = document.createElement("DIV");
        fieldset.id = "fieldset";
        var header = document.createElement("DIV");
        header.id = "header";
        span.appendChild(document.createTextNode(this.title));
        header.appendChild(span);
        div.appendChild(header);
        div.appendChild(this.content);
        div.appendChild(fieldset);

        return div;
    }
}

var myFieldset = new Fieldset();
myFieldset.getFieldset();

Answers


First you should create an instance of Fieldset, then you'll be able to call its functions (called methods):

var myFieldset = new Fieldset();

myFieldset.getFieldset();

    function Fieldset() {

    this.id = "";
    this.content = document.createElement("DIV");
    this.content.id = "content";
    this.title = "Title";

    this.getFieldset = function() {

        var div = document.createElement("DIV");
        div.id = this.id;
        var span = document.createElement("SPAN");
        //var fieldset = document.createElement("DIV");
        //fieldset.id = "fieldset";
        var header = document.createElement("DIV");
        header.id = "header";
        span.appendChild(document.createTextNode(this.title));
        header.appendChild(span);
        div.appendChild(header);
        div.appendChild(this.content);
        div.appendChild(fieldset);

        window.alert("test");

        return div;
    }

    //add call to run function
    this.getFieldset();

}

Need Your Help

How do I make Git ignore file mode (chmod) changes?

git ignore chmod

I have a project in which I have to change the mode of files with chmod to 777 while developing, but which should not change in the main repo.

iScroll Not Loading On A Hashed Link In jQuery Mobile

html css ajax jquery-mobile iscroll

I've set up an iScroll on a nested div in one of my detail pages in a jQuery mobile site. Basically what happens is when I click on the button on the main page (for an 'about' page) it goes to the