3

If functions have the scope, they should be executed within that scope, but here I think it's different See the code:

function foo() {
    var privateVal = "Private Val";
    this.publicVal = "Public Val";

    var privateAlert = function (str) {
        alert(str + this.publicVal);
        alert(str + privateVal);
    }

    this.Run = function () //see here
    {
        privateAlert("Private Call: ");

        this.publicAlert = privateAlert;
        this.publicAlert("Public Call: ");

        privateAlert = this.publicAlert;
        privateAlert("Private Call: ");
        this.publicAlert("Public Call: ");
    }
}

var bar = new foo();
bar.Run();

When the new object is created, Run() becomes the public method of an object or the method tht only belongs to the var bar. That method shouldn't be able to execute the privateAlert() function from within it; since function has the scope, it can only get executed from within the function it has been declared but this function have lost the scope where it was created and it is still getting executed . Clarify this please?

5
  • 2
    I don't even understand how you're confused so it's not easy to point your error. Commented Feb 5, 2013 at 16:53
  • 9
    Sounds like you're confused about closures - developer.mozilla.org/en-US/docs/JavaScript/Guide/Closures Commented Feb 5, 2013 at 16:54
  • 2
    there are no such things as "private methods" in javascript. if its a property (callable) on the object, it can be run by any code. The only way "private" can be achieved is by closure, which you've done with your "privateAlert" function. "Run" is equivalent to a public function in C#/Java, and would be considered akin to a "class member". Public members can indeed call private functions in higher-order languages, so why not here too? However, as @dystroy said, don't be quick to confuse javascript functions as true OOP methods Commented Feb 5, 2013 at 16:58
  • 1
    Closures are too often misunderstood. You should research and understand how they work to reinforce your knowledge of javascript functions. Commented Feb 5, 2013 at 17:01
  • 1
    Not sure if this is part of the question, but alert(str + this.publicVal); won't do what you think it will. privateAlert was called on no object, so this will be either null (in strict mode) or window (in non-strict mode). You can read a bit more about context here. Commented Feb 5, 2013 at 17:04

1 Answer 1

6

The simple explanation:

  1. Any variable declared inside a function is not accessible outside that function.
  2. Inner functions have access to variables declared on their outer scopes (see closures).

So, you can call privateAlert from Run because both have been defined inside of foo.

One more thing: Run is not a private method of bar, it's a public method.

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.