简体   繁体   English

单击后如何将函数返回值分配给变量以供以后在javascript中使用?

[英]How to assign the function return value to a variable after a click for later use in javascript?

So, what I want is to save the function return value on a variable for later use for example, just to save it, but after clicking, say on a button. 因此,我想要的是将函数返回值保存在变量上,以供以后使用,例如,只是保存它,但是单击后说一个按钮。 For example, we have: 例如,我们有:

let x = 3;    
function foo(){
        let z = 5;
        z = x + 3;
        return z;
    }

Now, if I write: 现在,如果我写:

let hold = foo();

it will keep the return value, but I want this to be saved on "hold" after click, by using something like this: 它会保留返回值,但是我希望通过单击以下命令将其保存在“保持”状态:

document.getElementById("empty_field").onclick = function() {foo()};

Here, the click calls the function, but it does not save the return value anywhere. 在此,单击会调用该函数,但不会在任何地方保存返回值。 How can I save it on a variable? 如何将其保存在变量中?

Short answer: You can't 简短答案:您不能

Long Answer: You can leverage the concept of scope to your advantage by simply declaring hold prior to setting it's value. 长答案:您可以通过简单地在设置其值之前声明hold来利用范围的概念,从而发挥自己的优势。 Here's what it would look like. 这就是它的样子。

let x = 3;
let hold; //variable is declared here so the click function has access to it.    
function foo(){
        let z = 5;
        z = x + 3;
        return z;
    }

document.getElementById("empty_field").onclick = function() {
    hold = foo()
};

Longer Answer: You can access or modify hold anywhere within the scope that the variable was declared. 更长的答案:您可以在声明该变量的范围内的任何位置访问或修改hold Let's add another function where we modify hold again. 让我们添加另一个函数,在其中我们再次修改hold

let x = 3;
let hold; //variable is declared here so the click function has access to it.    
function foo(){
        let z = 5;
        z = x + 3;
        return z;
    }

document.getElementById("empty_field").onclick = function() {
    hold = foo()
};

document.getElementById("increment").onclick = function(){
    hold++;  //this the same as hold = hold + 1
}

This is also valid because these click functions are being defined in the same scope that hold was defined in. It's good to avoid using globally scoped variables. 这也是有效的,因为这些单击函数是在定义hold范围内定义的。最好避免使用全局范围的变量。 So you may consider using a constructor and just passing the object between functions as an argument like so... 因此,您可以考虑使用构造函数,并仅在函数之间传递对象作为参数,例如...

function myConstructor(){
    //initialize
    this.hold = 0
    this.foo = function(x){
        let z = 5;
        z = x + 3;
        this.hold = z;
    }
    this.increment = function(){
        this.hold++;
    }
}

let x = 3;
let myObj = new myConstructor() //I know my naming sucks just bear with me lol

document.getElementById("empty_field").onclick = function() {
    myObj.foo(x) //x is still available since it was declared globally
    console.log(myObj.hold);
};

document.getElementById("increment").onclick = function(){
    myObj.increment();
    console.log(myObj.hold);
}

Anyways that's the long and short of it. 无论如何,这就是长短。 As always, there are many, many ways of doing things and there's not an absolute correct way. 与往常一样,有很多很多的做事方式,并且没有绝对正确的方式。 I just thought it might be helpful to know of a few options. 我只是认为了解一些选择可能会有所帮助。 More reading about constructors can be done here: https://www.w3schools.com/js/js_object_constructors.asp 可以在这里完成有关构造函数的更多阅读: https : //www.w3schools.com/js/js_object_constructors.asp

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

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