js面试题积累

这是一篇关于JavaScript面试题的积累,涵盖了window对象属性、事件监听、表达式求值、类型判断等多个知识点,适合准备JS面试者查阅和学习。

js相关面试题积累。。

 

1.写出window.foo 的值:

(window.foo || (window.foo = 'bar'));

解:(需注意:先读()内的值。。)

(window.foo || (window.foo = 'bar'));
console.log(window.foo);
//输出 : 
// bar

2.使用原生js, addEventListener,给每个li元素绑定一个click事件,并输出他们的顺序

<style>
    * {
      margin: 0;
      padding: 0;
    }
    li {
      list-style: none;
      background: green;
      height: 40px;
    }
    li:nth-child(2n) {
      background: red
    }
  </style>
  <ul>
    <li>a</li>
    <li>a</li>
    <li>a</li>
    <li>a</li>
  </ul>
  <script>
    function test() {
      var liCollection = document.getElementsByTagName('li');
      for (var i = 0; i < liCollection.length; i++) {
        (function (i) {
          liCollection[i].addEventListener('click', function () {
            console.log(i);
          }, false);
        }(i));
      }
    }
    test();
  </script>

 
3.
在这里插入图片描述

//1.
function retBytesLen(target) {
      var count = 0;
      for (var i = 0; i < target.length; i++) {
        if (target.charCodeAt(i) <= 255) {
          count++;
        } else if (target.charCodeAt(i) > 255) {
          count += 2;
        }
      }
      console.log(count);
    }

    retBytesLen('abcde');

//2.
function retBytesLen(target) {
      var count = target.length;
      for (var i = 0; i < target.length; i++) {
        if (target.charCodeAt(i) > 255) {
          count++;
        }
      }
      console.log(count);
    }

    retBytesLen('abcde加加');

//3.
function retBytesLen(target) {
      var count, len;
      count = len = target.length;
      for (var i = 0; i < len; i++) {
        if (target.charCodeAt(i) > 255) {
          count++;
        }
      }
      console.log(count);
    }

    retBytesLen('abcde加加');

 
4.写出下程序的执行结果:

var f = (1,2); 返回 2;返回逗号后面的那位;

// f = (a,b)  ==  f = b
var f = (
      function f() {
        return "1";
      },
      function g() {
        return 2;
      }
    )();

    console.log(typeof f);

    // number

 
5.写出下程序的执行结果:

//function f() {} 放在()中是表达式 不再是个函数。。
//未声明变量放在 typeof 中不会报错 返回 undefined
	var x = 1;
    if (function f() {}) {
      x += typeof f;
    }
    console.log(x);

    // 1undefined

 
6:

var str = "abc";
    str += 1; // abc1
    var test = typeof (str); //String
    if (test.length == 6) {
      test.sign = "typeof返回结果可能是String";
      // new String(test).sign = "..."; delete;
    }
    console.log(test.sign); 
    //undefined

 

7:

function foo() {
      bar.apply(null, arguments);
    }

    function bar() {
      console.log(arguments);
    }
    foo(1, 2, 3, 4, 5);

    //Arguments(5) [1, 2, 3, 4, 5]

 

8:typeof能返回结果的有:

string、number、boolean、undefined、object、function
 

9:

function b(x, y, a) {
      arguments[2] = 10;
      alert(a);
    }
    b(1, 2, 3);
    a = 10;
    alert(arguments[2]);

    // 10
    // arguments is not defined

 
10:isNaN() 函数用于检查其参数是否是非数字值
undefined == null true
undefined === null false
isNaN(‘100’) false
parenInt(‘1a’) == 1 true
{} == {} false //对象是代表两个房间所以不相等;

11.

function print() {
      console.log(foo);
      var foo = 2;
      console.log(foo);
      console.log(hello);
    }
    print();

    // undefined 
    // 2 
    // hello is not defined

12.

function print() {
      var test;
      test();

      function test() {
        console.log(1);
      }
    }
    print();

    // 1

 
13:

function print() {
      var x = 1;
      if (x == '1') console.log('one!');
      if (x === '1') console.log('two!');
    }
    print();

    // one!

 
14:

function print() {
      var marty = {
        name: 'marty',
        printName: function () {
          console.log(this.name);
        }
      }
      var test1 = {
        name: 'test1'
      };
      var test2 = {
        name: 'test2'
      };
      var test3 = {
        name: 'test3'
      };
      test3.printName = marty.printName;  // func
      var printName2 = marty.printName.bind({
        name: 123
      }); //123
      marty.printName.call(test1); // test1
      marty.printName.apply(test2); //test2
      marty.printName(); //marty
      printName2();  //123
      test3.printName(); //test3
    }
    print();

    // test1
    // test2
    // marty
    // 123
    // test3

 
13:

var bar = {
      a: '002'
    };

    function print() {
      bar.a = 'a';
      Object.prototype.b = 'b';
      return function inner() {
        console.log(bar.a);
        console.log(bar.b);
      }
    }
    print()();

    // a
    // b

 
14:

 
15:
 

 
持续更新ing。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值