3

I've been looking at how to not use Global Variables http://www.yuiblog.com/blog/2007/06/12/module-pattern/

What I'm not getting is how to use 'this' to access a public variable within my returned object.

console.log(this.myPublicProperty);

But if I use:

console.log(MYAPP.myProject.myModule.myPublicProperty);

I'll see the variable in the log.

I get 'undefined' when I try and access that public variable. Am I missing something that's not shown in the example code?

Thanks!

var MYAPP = {};
MYAPP.myProject = {};
MYAPP.myProject.myModule = function () {

    //"private" variables:
    var myPrivateVar = "I can be accessed only from within MYAPP.myProject.myModule.";

    //"private" method:
    var myPrivateMethod = function () {
        console.log("I can be accessed only from within MYAPP.myProject.myModule");
    }

    return  {
        myPublicProperty: "I'm accessible as MYAPP.myProject.myModule.myPublicProperty.",
        myPublicMethod: function () {
            console.log("I'm accessible as MYAPP.myProject.myModule.myPublicMethod.");

            //Within myProject, I can access "private" vars and methods:
            console.log(myPrivateVar);
            console.log(myPrivateMethod());

            //The native scope of myPublicMethod is myProject; we can
            //access public members using "this":
            console.log(this.myPublicProperty);
        }
    };
}(); // the parens here cause the anonymous function to execute and return
6
  • 1
    How and when are you trying to access your public property? Can you show some code. Commented Dec 12, 2011 at 15:36
  • 1
    The native scope of myPublicMethod is myProject: No, it's MYAPP.myProject.myModule, if you can even say this. It depends on how myPublicMethod is called. Commented Dec 12, 2011 at 15:38
  • @Zoidberg I don't really have anything I'm doing yet, just trying to understand how it works. Except for the first 2 lines I lifted the code from the YUI blog entry, just changed YAHOO to MYAPP and I'm testing in TextMate and Chrome. Commented Dec 12, 2011 at 16:03
  • Maybe it's because I used console.log where the blog entry has YAHOO.log? I haven't tried to use this.myPublicProperty inside the object, but I'll try that instead of trying to print to the log. I was feeling good about understanding this until I got to that last bit about //access public members using "this": Commented Dec 12, 2011 at 16:07
  • console.log(myPrivateMethod()) logs undefined because the myPrivateMethod logs something and then implicitly returns undefined (because it does not return anything). So that log will evaluate to console.log(undefined). The last console.log with this.myPublicProperty has no problems; at least I cannot reproduce any issues with this. Commented Dec 12, 2011 at 16:28

1 Answer 1

1

I don't like using this and dynamic binding to access a public variable, since it can break if I pass one of my functions as a callback or something like that. I prefer to have the references in my modules be static:

var M = { //explicit name
    f1: function(){ return M.f2(); },
    f2: function(){ }
};

return M;
Sign up to request clarification or add additional context in comments.

Comments

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.