您现在的位置:首页 >> 前端 >> 内容

关于函数提升的面试题

时间:2017/12/8 13:39:40 点击:

  核心提示:Question 1var a = 1;function b() { a = 10; return; function a() {}}b();console.log(a);Question 2func...

Question 1

var a = 1;
function b() {
     a = 10;
     return;  
     function a() {}
}
b();
console.log(a);

Question 2

function foo(){
   function bar() {
       return 3;
   }
   return bar();
   function bar() {
       return 8;
   }
}
alert(foo());

Question 3

function parent() {
   var hoisted = "I'm a variable";
   function hoisted() {
       return "I'm a function";
   }
   return hoisted(); 
}
console.log(parent());

Question 4

alert(foo());
function foo() {
   var bar = function() {
      return 3;
   };
   return bar();
   var bar = function() {
      return 8;
   };
}

Question 5

var myVar = 'foo';
(function() {
    console.log('Original value was: ' + myVar);
    var myVar = 'bar';
    console.log('New value is: ' + myVar);
})();

答案

输出: 1
输出: 8
输出: “TypeError: hoisted is not a function”
输出: 3
输出: 5

GitHub

作者:网络 来源:Web前端精髓