Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9242247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:32:36+00:00 2026-06-18T08:32:36+00:00

If functions have the scope, they should be executed within that scope, but here

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:32:37+00:00Added an answer on June 18, 2026 at 8:32 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have functions that return an object, but I'm confused that should I return
I have two functions that have different enough logic but pretty much the same
Javascript functions are objects and they have scope chain associated with them. If some
I keep getting errors that my functions have been defined multiple times. Of course
I've been using debug() more often now, but sometimes I wonder which functions have
I have functions that contribute to small parts of a figure generation. I'm trying
I have functions in place that will convert the results of sql queries into
I'm having a bit of a jQuery problem. Basically I have some functions that
I have a 3rd party JavaScript application that declares several global functions and needs
I quite often have optional arguments in functions, but some testing is showing a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.