前端常见面试题二十四(编程题)

第一题

  function Foo() {
    getName = function () {
      console.log(1);
    }
    return this;
  };
  Foo.getName = function () {
    console.log(2)
  };
  Foo.prototype.getName = function () {
    console.log(3)
  };
  var getName = function () {
    console.log(4)
  };
  function getName() { console.log(5) };

  Foo.getName() //2
  getName() //4
  Foo.getName() //2
  getName(); //4
  new Foo.getName(); //2
  Foo().getName(); //1

第二题

 for (var i = 0; i < 5; i++) {
    (function () {
      setTimeout(() => {
        console.log(i)
      }, i * 1000);
    }())
  } 
//answer => 5 5 5 5 5

第三题

  var number = 50;
  var obj = {
    number: 60,
    getNumber: function () {
      var number = 70;
      return this.number;
    }
  }
  console.log(obj.getNumber()); //60
  console.log(obj.getNumber.call()); //50
  console.log(obj.getNumber.call({ number: 20 }))  //20

第四题

 var y = 10;
  if (!(x in window)) {
    var x = 10;
  } else {
    ++y;
  }
  console.log(x); //undefined
  console.log(y); //11

你可能感兴趣的:(前端常见面试题二十四(编程题))