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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:33:53+00:00 2026-06-10T15:33:53+00:00

Assume I have a simple object in js with one private variable: function test(){

  • 0

Assume I have a simple object in js with one private variable:

function test(){
var value=true;
}

and now I want to create one instance:

var r=new test() //I want to get r === true

How can I return a value from it?

If I write:

function test(){
var value=true;

return value;
}

I have a test {} in result.

If I write:

function test(){
var value=true;

return function(){ return value; } 
}

then I can get the value, but I must add additional parentheses:

var r=new test()() //r === true

I don’t want the parentheses, so I tried to change the code to:

function test(){
var value=true;

return (function(){ return value; } )();
}

But in response, again I get test {}
How to write the return statement in this situation?

  • 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-10T15:33:55+00:00Added an answer on June 10, 2026 at 3:33 pm

    First I feel obliged to clarify a possible misunderstanding:

    function test(){
        var value=true;
    }
    

    is not an object with a private variable. It is a function with a local variable. When you call the function with new, it creates an object inheriting from the functions’s prototype with no properties. If you call the function normally, it simply executes the function body and returns undefined (since you are not returning anything).


    Solutions:

    Do you actually need a constructor function? I’m asking because your example is very simple. Obviously you cannot have the function return two values, true and the object.

    So, you could just call the function without new:

    function test() {
        var value = true;
        return value;
    }
    
    var r = test();
    

    If you really want r to be true then I see no reason to call the function as a constructor function.

    The reason why you got test {} as result was because you called the function with new. If you do that, the function will always return an object and if you don’t do so explicitly (value is a boolean, not an object), it implicitly returns this (which is an object).
    So again, if you really want r to be equal to value from inside the function, then simply don’t call the function with new.


    If you need an object though, there are a couple of ways:

    You can assign the value to a property and access it instead, like PokeHerOne showed in his answer or add a function which returns that value, as papaiatis demonstrates. The advantage is that the value is accessed explicitly and other people looking at your code understand what’s going on.

    Additionally, depending on what you want to do with that value / object, you can implement the valueOf methods, which gets called by various operators.

    For example:

    function Test(){
        var value = true;
    
        this.valueOf = function() {
            return value;
        }
    }
    
    var t = new Test();
    console.log(t);          // logs the Test instance
    console.log(t == true);  // logs `true`
    

    I.e. t is an object but behaves like the value true (value) in various operations. This is powerful but can also be quite confusing, since the type conversion is somewhat implicit and it’s not something that is used in JavaScript very often.

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

Sidebar

Related Questions

Assume I have an enumerable object enum and now I want to get the
Let's assume we have a simple internet socket, and it's going to send 10
Assume I have two tables, Student Test Id Name TestId Type StudentId -- ----
Assume I have the following ; def test(): while 1: a = b time.sleep(60)
Assume you have a function read_key and normally it does some stuff. You someone
I have a really simple association with the Devise user object where each user
I have a thread which produces data in the form of simple object (record).
lets say, i have two tables, one for object records and one for activity
Say you have a very simple data structure: (personId, name) ...and you want to
Assume that I have the following object public class MyClass { public ReadOnlyDictionary<T, V>

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.