0
myvar = whatever;

namespace.myspace.Create = function () {
    this.myvar = null;
    this.MyFunction = function (val) {
        this.myvar = val;
    }
}

---

namespace.myspace.Create = function () {
    var myvar = null;
    this.MyFunction = function (val) {
        this.myvar = val;
    }
}

---

namespace.myspace.Create = function () {
    myvar = null;
    this.MyFunction = function (val) {
        this.myvar = val;
    }
}

var myObject = new namespace.myspace.Create();

Looking at the above code, especially the myvar being defined as:

  • this.myvar = null;
  • var myvar = null;
  • myvar = null;

What are the main differences, scopes and uses of these? Any pitfalls or preferred ways of doing? Any of these just flat out wrong and should not be used?

2
  • myvar = whatever global. this.myvar = null context or instance. var myvar = null local. myvar = null global. Commented Sep 26, 2013 at 8:55
  • Which one you'd use depends on what you're trying to achieve. Note that all three of your examples set an instance variable with this.myvar inside this.MyFunction(). Commented Sep 26, 2013 at 8:55

1 Answer 1

1

myvar is a global variable

this.myvar is a public variable scoped on the myspace namespace

and var myvar is a private variable that is also scoped on the myspace namespace

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.