如何在函数B()中更改函数A()中的x值
function A() {
var x = 10; // Value to be changed
function B() {
var x = 20;
// From here i want to change the value of x (i.e. x=10 to x=40)
}
B();
}
A();
Run Code Online (Sandbox Code Playgroud)
0x4*_*2D2 10
var打算覆盖变量时不要使用.使用var创建一个新变量,该变量在声明它的作用域的本地.这就是为什么x不在外面改变的原因.
function A() {
var x = 10;
function B() {
x = 20; // change x from 10 to 20
}
B(); // x is now changed
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7178 次 |
| 最近记录: |