2017-09-11 17 views
1
first(); 
function first(){ 
    second(); 
    third(); 
} 
function second(){ 
    var b='second'; 
} 
function third(){ 
    console.log(b); 
} 

3番目の変数bにアクセスしようとしているときにエラーが発生しました。 console.log(b); ^ノードjs:メソッド間でローカル変数にアクセスする方法

にReferenceError:あなたはbにアクセスしたい場合は、グローバル

first(); 
var b; 
    function first(){ 
    second(); 
    third(); 
} 
function second(){ 
    b='second'; 
} 
function third(){ 
    console.log(b); 
} 

console.log(b); 

としてそれを定義するために持っているよりもBが

+1

を見てあなたは読みたいと思うかもしれません[ Javascriptの変数の範囲は何ですか?](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – Paul

答えて

0

が定義されていませんが、私のビンglobal b

+0

ありがとうございます、うまくいきます。 –

関連する問題