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

  • Home
  • SEARCH
  • 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 6805081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:32:38+00:00 2026-05-26T19:32:38+00:00

I am trying to organize my code using the revealing module pattern. I have

  • 0

I am trying to organize my code using the revealing module pattern.
I have a very basic question about how to set up a setter method.

$(document).ready(function() {  
    var designs = (function() {  
        var curRow,  
            setCurRow = function(val) {  
                curRow = val;  
            },
            initTable = function() {  
                setCurRow(0);
            };
        return {
            curRow : curRow,
            setCurRow : setCurRow,
            initTable : initTable
        }
    }) ();  
    designs.initTable();  
    designs.setCurRow(someNewVal);
    console.log(designs.curRow);
});

The problem is that i dont get the someNewVal in the console output, I get undefined instead! I have a feeling I am doing something pretty silly here.

  • 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-05-26T19:32:38+00:00Added an answer on May 26, 2026 at 7:32 pm

    You can also solve this in another way by understanding the scopes of the variables and functions involved.

    When you return your object constructor { curRow: curRow ... }, that just initializes the object member named curRow to the value of the variable curRow in the scope of the anonymous function; it doesn’t create any persistent connection between them.

    Once the anonymous function returns, calling designs.setCurRow is updating the curRow variable in that scope exactly as you expect, but that variable is now totally inaccessible to the outside world — there is no connection between it and the curRow member of designs.

    You can solve this by making the setCurRow method operate on this.curRow, as in the other solutions. In that case you don’t need to make curRow a variable in the original scope, since it’s entirely unused. The other solution is to add a ‘getter’ method to your current one:

    var designs = (function() {  
        var curRow,  
        setCurRow = function(val) {  
            curRow = val;  
        },
        getCurRow = function() {
            return curRow;
        },
        initTable = function() {  
            setCurRow(0);
        };
        return {
            getCurRow : getCurRow,
            setCurRow : setCurRow,
            initTable : initTable
        };
    }) ();  
    designs.initTable();  
    designs.setCurRow(someNewVal);
    console.log(designs.getCurRow());
    

    Because getCurRow and setCurRow are functions that are closed in the scope containing the variable varRow, they can reach back into that scope and access and change variables that are only accessible within it.

    In this case making curRow a member of the object you return is probably simpler, but the other way is useful too since you can use it to create effectively private members and methods.

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

Sidebar

Related Questions

I guess i have a simple question about better code organization. Say i have
I am trying to create a small server type application and have a question
I'm trying to organize my code flexible. So my application makes heavy use of
Trying to sort an array by writing my own sort method using recursion (Pine's
How do you like to organize your Drupal code? One giant module? Separate modules
I'm trying to wrap my head around organising my code. I have several modules
I am trying to organize my code and move repetitive functions to a single
I'm trying to create something short.com/ASWi7 -> short.com/index.php?h=ASWi7 so I tried using this code
I am trying to organize a Zip Archive into an array so that I
I'm trying to jump through some hoops to organize data in a special way.

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.