开发者社区 问答 正文

求 num 的值

// 面试题1
var num = 123;
function f1() {
  console.log(num); 
}
function f2() {
  var num = 456;
  f1();
}
f2();

// 面试题1 变式
var num = 123;
function f1(num) {
  console.log(num); 
}
function f2() {
  var num = 456;
  f1(num);
}
f2();

// 面试题1 变式
var num = 123;
function f1() {
  console.log(num);
}
f2();
function f2() {
  num = 456; //这里是全局变量
  f1();
}
console.log(num); 

展开
收起
kun坤 2019-11-28 14:58:59 324 分享 版权
1 条回答
写回答
取消 提交回答
  • // 面试题1
    var num = 123;
    function f1() {
      console.log(num); // 123
    }
    function f2() {
      var num = 456;
      f1();
    }
    f2();
    
    // 面试题1 变式
    var num = 123;
    function f1(num) {
      console.log(num); // 456
    }
    function f2() {
      var num = 456;
      f1(num);
    }
    f2();
    
    // 面试题1 变式
    var num = 123;
    function f1() {
      console.log(num); // 456
    }
    f2();
    function f2() {
      num = 456; //这里是全局变量
      f1();
    }
    console.log(num); // 456
    
    
    2019-11-28 14:59:07
    赞同 展开评论
问答地址: