最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Access outer function variables from inner function - Stack Overflow

programmeradmin0浏览0评论

How to change value of x in function A() from function B()

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();

How to change value of x in function A() from function B()

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();
Share Improve this question edited Apr 29, 2019 at 8:47 Siu Ching Pong -Asuka Kenji- 8,2039 gold badges54 silver badges75 bronze badges asked Sep 18, 2012 at 11:19 CoolenoughCoolenough 5331 gold badge7 silver badges16 bronze badges 4
  • 1 Easy, just name your other variable y. – elclanrs Commented Sep 18, 2012 at 11:21
  • its a interview question both vaiables should be named as x – Coolenough Commented Sep 18, 2012 at 11:22
  • can you, inside B(), return 40;, then when calling B: x = B(); – jeremy Commented Sep 18, 2012 at 11:27
  • @Siu Why was it necessary to edit every post here without making any actual improvements? See When should I make edits to code? – Nick is tired Commented Apr 29, 2019 at 9:02
Add a comment  | 

4 Answers 4

Reset to default 10

Do not use var when intending to overwrite the variable. Using var creates a new variable, local to the scope in which it is declared. That's why x is not changing on the outside.

function A() {
    var x = 10;
    function B() {
        x = 20; // change x from 10 to 20
    }

    B(); // x is now changed
}

If I understand your question, the following code is a solution:

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)
        changeX(40);
    }
    function changeX(y) {
        x = y;
    }
    B();
    alert(x);
}

A();

However there are more elegant approach, but it depends on your application.

Maybe:

function A() {
    var x = 10; // Value to be changed
    function B() {
        var x = 20;
        return x; // Return new value of x
    }
    x = B(); // Set x to result returned by B(), i.e. the new value
}

A();

the var statement will create new local variable. So in your example:

function A() {
    var x = 10; // You can think of it as A.x
    function B() {
        var x = 20; // And this as A.B.x
    }
}

Those two variables belong to different scope, if you want to access variable of an outer scope from within inner scope just access it, without re-declaring it.

You might want to checkout "JavaScript Language Specifications" document available at http://www.planetpdf.com/codecuts/pdfs/tutorial/jsspec.pdf to understand how scopes, statements and other basics work in JavaScript.

发布评论

评论列表(0)

  1. 暂无评论