Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Explain this JS syntax

Status
Not open for further replies.

organicg

Programmer
Oct 21, 2002
151
0
0
US
This is a snippet of widget code that was provided as a sample that I'm trying to understand the syntax of. JSLint.com js parser doesn't like the use of 'public' and 'private' keyword in var declarations, though the script is working fine in my widget. If I rename 'public' and 'private' then JSLint throws a different error. Seems like it's JSON syntax, and interesting that 'public' is returned at the end. Can anyone help explain? I've cut out the AJAX functionality to keep it simple.

Code:
var Widgets = Widgets ? Widgets : function() {

    var private = {

        host: null,
        container: null,

        loadhtml: function(container) {
            private.container = container;
        }
    };

    var public = {

        Init: function(container) {
            private.container = document.getElementById(container);
            private.loadhtml(private.container);
        },

        callbackMethod: function(data) {
            if (!data) return;
            var div = private.container;
            div.innerHTML = data;  // assign new HTML into #ROOT
            div.style.display = 'block'; // make element visible
            div.style.visibility = 'visible'; // make element visible
        }
        
    }

    return public;
} ();
 
In this case, public and private are variable names. I wonder if your parser is interpreting them as keywords? Try renaming the variables to something else, such as private1 or public1 to see what happens.

---------------------
He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
I don't really see any syntax errors; try placing a semi-colon after the closing brace of the public object declaration ... code should work though.

Greg
 
So it's OK to use reserved words as variable names, that would never compile in C#?

If I rename 'private' JSLint.com says, "'private1' has not been fully defined yet." because it doesn't like 'private1' used within its own var declaration a few lines down:
private1.container = container;

Is separating the code into these 'public' and 'private' var structures just for clarity, or are they actually scoped as such? Is that why 'public' is returned at the end?

Added the semicolon, per grtammi, and JSLint.com says about the very last line:
} ();
"Wrap an immediate function invocation in parentheses to assist the reader in understanding that the expression is the result of a function, and not the function itself."

I know that's a lot of questions...whatever you guys can explain is appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top