简体   繁体   English

从js中的对象返回值

[英]return a value from object in js

Assume I have a simple object in js with one private variable: 假设我在js中有一个带有一个私有变量的简单对象:

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 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? 但是作为回应,我再次得到测试{}在这种情况下如何编写return语句?

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. 当您使用new调用该函数时,它将创建一个从该函数的原型继承而没有属性的对象。 If you call the function normally, it simply executes the function body and returns undefined (since you are not returning anything). 如果正常调用该函数,则它仅执行函数主体并返回undefined (因为您未返回任何内容)。


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. 显然,您不能让函数返回两个值,即true和object。

So, you could just call the function without new : 因此,您可以不使用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. 如果您真的希望rtrue那么我认为没有理由将该函数称为构造函数。

The reason why you got test {} as result was because you called the function with new . 之所以得到test {}原因是因为您使用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). 如果这样做,该函数将始终返回一个对象,并且如果您未显式这样做( value是布尔值,而不是对象),则该函数将隐式返回this对象(这是一个对象)。
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 . 如此反复,如果你真的想r等于value从函数内部,后来干脆不调用该函数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 . 您可以将值分配给属性并访问它,如PokeHerOne他的答案中显示的那样,或添加一个返回该值的函数,如papaiatis所 展示 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. 此外,根据您要对该值/对象执行的操作,可以实现valueOf方法,该方法可被各种运算符调用。

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. t是一个对象,但在各种操作中的行为类似于值truevalue )。 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. 这很强大,但也可能造成很大的混乱,因为类型转换在某种程度上是隐式的,并且不是JavaScript中经常使用的类型。

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: 由于value定义为私有,因此无法从外部访问:

alert(t.value) // undefined

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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