简体   繁体   中英

return a value from object in js

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?

I believe you need to do something like:

function test(){
    this.value = true;
}

and then

var r=new test();
if (r.value == true) {
    //Do something
}

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`

Ie 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.

Used methods defined internally:

function TestClass(){
    var value = true;
    this.getValue = function(){
        return value;
    };
}

var t = new TestClass();
alert(t.getValue()); // true

Since value is defined as private it is not accessible from outside:

alert(t.value) // undefined

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM