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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:19:01+00:00 2026-06-09T01:19:01+00:00

I’m trying to understand how the this keyword works. This article The this keyword

  • 0

I’m trying to understand how the this keyword works.

This article The this keyword states

“In JavaScript this always refers to the “owner” of the function we’re executing, or rather, to the object that a function is a method of.”

function someClosure() {
    var myVal0, myVal1, myVal2;

    init = function (myVal0, myVal1, myVal2) {

        myVal0 = myVal0;
        this.myVal1 = myVal1;
        this.myVal2 = myVal2;

    };

    getMyVal0 = function() { return myVal0 };
    getMyVal1 = function() { return myVal1 }
    getMyVal2 = function() { return this.myVal2 }
};

I surmise that getMyVal0 is undefined after init() is called because of a naming clash (the assignment is ambiguous).

But (after calling init()) why does getMyVal1 return undefined? The reference to myVal1 should not be ambiguous. Does Javascript require an explicit use of this? getMyVal2 returns the expected value, but again, I’m surprised that I need the explicit this.

Please clarify this behavior.

Ultimately, I’m trying to establish a naming convention for function arguments when initializing member variables. By industry convention, IRR is IRR and it seems this should allow me to avoid comming up with two names for a variable (without always referencing the member variable with this.) What’s the convention for doing what I want to do?

  • 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-09T01:19:03+00:00Added an answer on June 9, 2026 at 1:19 am

    Read this introduction to the this keyword at MDN.

    The value of this depends on how you call the function. But you don’t call the init at all, and all variables are still undefined.

    By industry convention, this should allow me to avoid comming up with two names for a variable

    No. this in JavaScript is different from this in other languages like Java. It will not let you access variables from a higher scope. In your case, the arguments of the init function just shadow the variables from myClosure – you can’t access them. You need to rename them if you want them. Use this script:

    var init, getMyVal; // global, or at least "outer", variables
    
    function closure() {
        var myVal, myVal2; // scoped to the closure
    
        // all the following function can access them
        init = function(val, myVal2) {
            myVal = val; // assign the argument to the closure variable
            // myVal2 === myVal2 - sorry, this only refers to the argument
        };
        getMyVal = function() {
            return myVal; // get the closure variable
        };
    };
    closure(); // execute it - you might also have used a immediately-executing function
    
    init("some value");
    
    getMyVal(); // "some value"
    

    Btw, you might combine the closure and init call to only one function:

    var getMyVal;
    function initClosure(myVal) {
        // the argument is scoped to this function, like a var declaration
    
        getMyVal = function() { return myVal; };
    }
    initClosure("some value");
    
    getMyVal(); // "some value"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,

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.