js 从一个函数中传递值到另一个函数

一个函数的调用大家都会用,今天在调接口的时候突然发现需要引用个另一个函数中拿到的值

 

举个例子

刚开始,我是这样调用的 

<script type="text/javascript">
    function a(){
    	var b = 'hello world';
    	return b;
    }
    function c(){
    	return a();
    }
    alert(c());
</script>

   alert弹出的是 hello world 。

 

但是我a函数内部还有一个函数,画风是这样的

<script type="text/javascript">
    function a(){
    	function aa(){
    		var b = 'hello world';
    		return b;
    	}
    }
    function c(){
    	return a();
    }
    alert(c());
</script>

   这次alert出来的就是想要的b值了,而是undefined !!

   解析:我在这里的时候拿到的一直都是undefined,当回过头看的时候可以发现

            aa函数中的b需要return,第一次return的时候,无疑是把b传给了a。但是a中的值并没有传出去,于是就有了下一步操作 

<script type="text/javascript">
    function a(){
    	function aa(){
    		var b = 'hello world';
    		return b;
    	}
    	return aa();
    }
    function c(){
    	return a();
    }
    alert(c());
</script>

    现在alert出来的就是我们想要的hello world了 ~~ 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

.

猜你喜欢

转载自570109268.iteye.com/blog/2414545