面试题

1、自执行函数

  var  f  =  (

    function  g ( ) {

      return  '111';

    },

    function  k ( ) {

      return 2;

    })( )

  console.log(f)  // 2 逗号返回最后一个表达式

2、函数变成表达式

  var  x  =  1;

  if(function  f ( ) { } ){

    x  +=  typeof  f;

  }

  console.log(x)  // 1undefined  函数变成表达式,判断完就消失了

3、包装类

  var  str  =  'abc';

  str  +=  1;

  var  test  =  typeof(str);

  if(test.length  == 6){

    test.sign  =  'aabbccdd'

  }

  console.log(test.sign)  // undefined

4、this指向问题

  var  name  =  '222';

  var  a  =  {

    name: '111',

    say: function ( ){

      console.log(this.name)

    }

  }

  var  b  =  {

    name:  '333',

    say: function ( fun ){

      fun( )

    }

  }

  b.say( a.say )  // 222

5、类数组模拟数组的push方法

  var  obj  =  {

    "2" : "a",

    "3" : "b",

    "length" : 2,

    "push" : Array.prototype.push

  }

  obj.push('c')

  obj.push('d')

  console.log(obj)

  {

    "2" : "c",

    "3" : "d",

    "length" : 4,

    "push" : Array.prototype.push

  }

  数组push方法原理

  Array.prototype.push  =  function (target){

    this[this.length]  =  target;

    this.length ++;

  }

6、. VS = 操作符优先级

  let a = {n : 1};

  let b = a;

  a.x = a = {n: 2};

  console.log(a.x)  // undefined

  console.log(b.x)  // {n: 2}

 7、考察自执行函数

  var b = 10;

  (function b(){

    // 'use strict'

    b = 20

    console.log(b)  // ƒ b(){ // 'use strict' b = 20 console.log(b)}

  })()

8、稀疏数组与密数组

  var ary = [0,1,2];

  ary[10] = 10;

  var  newAry  =  ary.filter(function(x) { return x === undefined;});

  console.log(newAry)  // [ ]

9、for循环中使用var初始化i和使用let初始化i

  使用var初始化变量i

    第一种:

    for(var  i  =  0;  i  <  5;  i  ++){}

    console.log(i)  // 5

    第二种:

    var  arr  =  [ ]

    for(var  i  =  0;  i  <  5;  i  ++){

      arr[ i ]  =  function( ){

        console.log( i )

      }

    }

    arr[ 2 ]( )  // 5

  使用let初始化变量i

    第一种:

    for(let  i  =  0;  i  <  5;  i  ++){}

    console.log(i)  // 报错  i  is  not  defined

    第二种:

    var  arr  =  [ ]

    for(var  i  =  0;  i  <  5;  i  ++){

      arr[ i ]  =  function( ){

        console.log( i )

      }

    }

    arr[ 2 ]( )  // 2

10、作用域

  (1)

    var  x  =  1;

    function  fun(x, y = x){

      console.log(y)

    }

    f(5)  // 5

  (2)

    var  x  =  1;

    function  fun(y = x){

      let x  =  8;

      console.log(y)

    }

    fun( )  // 1

  (3)

    function  fun(y = x){

      let  x  =  8;

      console.log(y)

    }

    fun( )  // 报错,x  is  not  defined

原文地址:https://www.cnblogs.com/cuishuangshuang/p/13264750.html