不应该是全局变量" a"变为20然后30

时间:2017-08-29 02:09:09

标签: r function

<div style="column-count: 3;">
  <ul>
    <li>lorem ipsum</li>
    <li>lorem ipsum</li>
    <li>lorem ipsum</li>
    <li>lorem ipsum</li>
    <li>lorem ipsum</li>
    <li>
      <ul>
        <li>
          <div>
            <h3>Education</h3>
            <a>Education 1</a>
          </div>
        </li>
      </ul>
    </li>
    <li><a>Education 2</a></li>
    <li><a>Education 3</a></li>
    <li><a>Education 4</a></li>
    <li><a>Education 5</a></li>
    <li><a>Education 6</a></li>
  </ul>
  <ul>
    <li>
      <ul>
        <li>
          <div>
            <h3>Careers</h3>
            <a>Careers1</a>
          </div>
        </li>
      </ul>
    </li>
    <li><a>Careers2</a></li>
    <li><a>Careers3</a></li>
  </ul>
  <ul>
    <li>
      <ul>
        <li>
          <div>
            <h3>Legal</h3>
            <a>Legal</a>
          </div>
        </li>
      </ul>
    </li>
    <li><a>Legal</a></li>
    <li><a>Legal</a></li>
  </ul>
</div>

►现在打印外部功能

h3

怎么可能&#34; a&#34;仍然70 ...&lt;&lt; - 正在改变全局变量 在外面&#34; a&#34;正在改变出局&#34; a&#34; ......在内心......&#34; a&#34;正在改变&#34; a&#34;在外()...所以怎么可能最大的&#34; a&#34;还是70 ??

1 个答案:

答案 0 :(得分:0)

考虑使用reprex()作为可重现的示例,这些示例会显示您的大括号未对齐且答案实际上是30

a <- 70

outer <- function(){
  a <<- 20
  inner <- function(){
    a <<- 30
    print(a)
  } # end of inner function
  inner() 
  print(a)

} # end of outer function
outer()
#> [1] 30
#> [1] 30
a
#> [1] 30