如何在另一个函数中声明的函数中使用变量 [英] How do I use a variable in a function which is declared in another function

查看:264
本文介绍了如何在另一个函数中声明的函数中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数F2,在函数F1中调用。我需要在F1中声明的F2中使用变量。我怎么能这样做?



我尝试过:



我试图在函数f2中使用变量,但我想这不会起作用。对于现在该做什么一无所知

I have a function F2 which is called in function F1. I need to use a variable in F2 declared in F1. How can I do this?

What I have tried:

I tried to use the variable in function f2 but I guess that wont work. Clueless as to what to do now

推荐答案

你不能:你在函数中声明的变量只能在它们所在的函数内可用(实际上只存在)声明。



要共享数据,您必须在任何函数(GLOBAL变量)之外声明变量,并在两个函数中使用它: JavaScript范围 [ ^ ]
You can't: the variables you declare in a function are only available (indeed only exist) inside the function in which they are declared.

To "share" data you would have to declare the variable outside any function (a GLOBAL variable) and use that in both functions: JavaScript Scope[^]


这篇关于如何在另一个函数中声明的函数中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆