javascript:在另一个函数中获取函数的变量值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11813806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:12:08  来源:igfitidea点击:

javascript: get a function's variable's value within another function

javascriptfunctionvariablescall

提问by Maria

okay so I have this code in body:

好的,所以我在正文中有这个代码:

<input type="text" value="haha" id="full_name"/>

And this code in script

脚本中的这段代码

<script language="javascript" type="text/javascript">
    function first(){
        var nameContent=document.getElementById('full_name').value;
    }

    function second() {
        first();
        y=nameContent; 
        alert(y);
    }
    second();
</script>

I want a alert showing the value of the element full_name, but it doesn't seem to work, does anyone know why? :S

我想要一个显示元素 full_name 值的警报,但它似乎不起作用,有人知道为什么吗?:S

回答by Blender

nameContentonly exists within the first()function, as you defined it within the first()function.

nameContent只存在于first()函数中,正如你在first()函数中定义的那样。

To make its scope broader, define it outside of the functions:

为了使其范围更广,请在函数之外定义它:

var nameContent;

function first(){
    nameContent=document.getElementById('full_name').value;
}

function second() {
    first();
    y=nameContent; alert(y);
}
second();

A slightly better approach would be to returnthe value, as global variables get messy very quickly:

一个稍微好一点的方法是return取值,因为全局变量很快就会变得混乱:

function getFullName() {
  return document.getElementById('full_name').value;
}

function doStuff() {
  var name = getFullName();

  alert(name);
}

doStuff();

回答by Marcelo Assis

Your nameContent scope is only inside first function. You'll never get it's value that way.

您的 nameContent 范围仅在第一个函数内。你永远不会以这种方式获得它的价值。

var nameContent; // now it's global!
function first(){
    nameContent = document.getElementById('full_name').value;
}

function second() {
    first(); 
    y=nameContent; 
    alert(y);
}
second();

回答by Jason Fingar

you need a return statement in your first function.

您需要在第一个函数中使用 return 语句。

function first(){
    var nameContent = document.getElementById('full_name').value;
    return nameContent;
}

and then in your second function can be:

然后在你的第二个函数中可以是:

function second(){
    alert(first());
}

回答by The Alpha

Your nameContentvariable is inside the function scope and not visible outside that function so if you want to use the nameContentoutside of the function then declare it globalinside the <script>tag and use inside functions without the varkeyword as follows

您的nameContent变量在函数范围内并且在该函数外不可见,因此如果您想使用nameContent函数的外部,则global<script>标记内声明它并使用不带var关键字的内部函数,如下所示

<script language="javascript" type="text/javascript">
    var nameContent; // In the global scope
    function first(){
        nameContent=document.getElementById('full_name').value;
    }

    function second() {
        first();
        y=nameContent; 
        alert(y);
    }
    second();
</script>

回答by Ricardo Mendoza

the OOP way to do this in ES5 is to make that variable into a property using the this keyword.

在 ES5 中执行此操作的 OOP 方法是使用 this 关键字将该变量变成一个属性。

function first(){
    this.nameContent=document.getElementById('full_name').value;
}

function second() {
    y=new first();
    alert(y.nameContent);
}