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 8751529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:03:55+00:00 2026-06-13T13:03:55+00:00

Where is the data supplied by the argument being stored? Is a var first

  • 0

Where is the data supplied by the argument being stored? Is a var first being created implicitly?

function Student(first){
   this.getFirst = function(){
     return first;
   }
}

Tested with:

var myStudent = new Student("ross");

console.log(myStudent); // Student { getFirst=function() }

console.log(myStudent.getFirst()); // ross

console.log(first); // reference error, first not defined

console.log(myStudent.first); // undefined

for(var x in myStudent){
console.log(x);
}
// getFirst

My second question is if I understand these correctly:

What happens with "var" variables inside a JavaScript Constructor?

“var” variables, "this" variables and "global" variables – inside a JavaScript Constructor

…is the getFirst function creates a closure and saves the state of the constructor’s parameter value, if I use a var first in the constructor body is it okay to think of that as ‘encapsulation’? Additionally, any inner function saves all the parameter values in a “closure state” or just the one’s referenced by the inner function?

Thank you very much for your thoughts. This is my first question on S.O. but use the site almost daily as a reference, so thank you for that. My programming knowledge is limited so pardon if I’ve used crappy terms, happy to clarify where needed.

  • 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-13T13:03:56+00:00Added an answer on June 13, 2026 at 1:03 pm

    The first parameter is stored locally to the function that is your constructor, so anywhere inside Student() it will be in scope.

    More interestingly, the the anonymous inner function that you’re assigning to this.getFirst is closing over that value. Because of that, the inner function maintains a reference to the first variable even after the constructor is finished executing.

    This works for regular functions too, not just constructors:

    function makeCounter() {
       var count = 1;
       return function() { return count++; };
    }
    
    var counter = makeCounter();
    console.log(counter());  // 1
    console.log(counter());  // 2
    console.log(counter());  // 3
    

    When used the right way, this approach can be used to achieve “private” variables in JavaScript, in the sense that the values captured in the closure are inaccessible from the outside.

    Because of that, it turns out not to matter whether a closure captures all the variables in its scope or just the ones it uses, since you can’t “reach inside” to those closed-over variables anyway. (Though in practice generally only the used values are captured, so that the runtime can garbage collect the rest, as they’re unneeded.)

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

Sidebar

Related Questions

Possible Duplicate: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result This is
Possible Duplicate: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result I've this
Apps such as FML, TWI etc that display user supplied data have the ability
data table contain column named as Fld_primary. this column contain value like 0.00 it
The below is throwing the following error: Warning: mysql_fetch_array(): supplied argument is not a
I've created a WCF DataService and for this service I require some custom authentication
I'm completely lost, I have this code: $val = explode('?',$_POST['data']); print_r($val); foreach($val as &$v)
Possible Duplicate: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in php
I get the array data like this: $tags = $_GET['item']['tags']; if ($tags){ foreach ($tags
This function worked fine a month ago, before the newest JQuery (1.5) came out.

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.