0

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();
2
  • I recommend to read through MDN - Working with Objects. Commented Jun 13, 2012 at 15:53
  • 1
    from where would you like to execute it? Commented Jun 13, 2012 at 15:54

2 Answers 2

5

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();
Sign up to request clarification or add additional context in comments.

3 Comments

I thought when running this it would make what I presumed myFieldset.getFieldset() would run which is create a DIV, SPAN, etc. Don't mean to make this out of scope, but shouldn't that write to the page?
@peteskiii: No. You don't append the created div anywhere. It is returned by getFieldset, so do something with it ;)
but when i run this code, nothing happens at all, i don't see anything in the object inspector?
-1
    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();

}

3 Comments

I think he means from OUTSIDE the implementation.
Since he didn't imply that, I didn't assume. You know what they say about assumptions.
I have tried to update the code example to better show what I am trying to achieve.

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.