Skip to content

Instantly share code, notes, and snippets.

@huanggm
Last active October 12, 2023 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huanggm/27313742abae65179d70b186e76957e2 to your computer and use it in GitHub Desktop.
Save huanggm/27313742abae65179d70b186e76957e2 to your computer and use it in GitHub Desktop.
面试题-笔试题记录

第一题:考察this和作用域

inner = 'window';

function say() {
    console.log(inner);
    console.log(this.inner);
}

var obj1 = (function() {
    var inner = '1-1';
    return {
        inner: '1-2',
        say: function() {
            console.log(inner);
            console.log(this.inner);
        }
    }

})();

var obj2 = (function() {
    var inner = '2-1';
    return {
        inner: '2-2',
        say: function() {
            console.log(inner);
            console.log(this.inner);
        }
    }

})();


say();

obj1.say();
obj2.say();

obj1.say = say;
obj1.say();

obj1.say = obj2.say;
obj1.say();

参考答案:

say(); //window window

obj1.say(); //1-1 1-2
obj2.say(); //2-1 2-2

obj1.say = say;
obj1.say(); //window 1-2

obj1.say = obj2.say;
obj1.say(); //2-1 1-2

第二题:考察css熟练度

  1. 计算margin塌陷高度
<div>
    <div style="margin: 10px 20px 30px;">
        <div style="margin: 15px 25px;">a</div>
    </div>
    <div style="margin: 10px 20px 30px 40px;">
        <div style="margin: 40px 30px 20px 10px;">b</div>
    </div>
</div>

问题是计算a与b的垂直间距,答案是40px

  1. 写出3种清除浮动的方式
//使用伪元素清除浮动
.clearfix:after{
    content:"";/*设置内容为空*/
    clear:both;/*清除浮动*/
    display:block;/*将文本转为块级元素*/
    height:0;/*高度为0*/
    visibility:hidden;/*将元素隐藏*/
}
.clearfix{
    zoom:1;/*为了兼容IE*/
}

开启BFC——overflow:hidden
开启BFC——float:left

第三题:使用vue实现Money组件,并且支持v-model使用

功能点是用户在input中输入数字是以元为单位的。但是v-model对应的变量则是以分为单位的。即相差100倍。 效果参考链接:https://p1sjc.csb.app/

使用组件的代码:

<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png" width="25%" />
    <Money v-model="money"></Money>
    <div>实际金额:{{ money }}分</div>
  </div>
</template>

<script>
import Money from "./components/Money";

export default {
  name: "App",
  components: {
    Money,
  },
  data() {
    return {
      money: 150,
    };
  },
};
</script>

算法题

  1. 两个有序数组的排序
  2. 数组中所有数字都出现了2次,只有一个数字出现1次,请找出这个数字
  3. 链表倒数第3个节点
  4. 笛卡尔乘积
    编写一个函数:function getAllStringByArray(arr) {return [xxx]}
    其中arr参数是一个二维数组,长度未知
    
    示例:getAllStringByArray([['a', 'b'], ['1', '2']])
    返回值是:['a1','a2','b1','b2',]
    
    示例:getAllStringByArray([['a', 'b', 'c'], ['1', '2', '3'], ['A', 'B', 'C']])
    返回值是: ['a1A','a2A','a3A','b1A','b2A','b3A','c1A','c2A','c3A','a1B','a2B','a3B','b1B','b2B','b3B','c1B','c2B','c3B','a1C','a2C','a3C','b1C','b2C','b3C','c1C','c2C','c3C',]
    
  5. 考察数组的方法
  • 5.1 给你一个数组[1,2,3,4,5],实现一个函数,返回一个新数组,每个数字都加上数字所在的index
  • 5.2 给你一个数组[1,2,3,4,5],实现一个函数,返回所有偶数的和
  • 5.3 实现一个Array.prototype.map方法
    var new_array = arr.map(function callback(currentValue[, index[, array]]) {
        // Return element for new_array 
    }[, thisArg])
    
  • 5.4 使用reduce实现Array.prototype.map方法
    arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])
  1. 考察setTimeout和setInterval的区别
  • 6.1 口头描述区别:1. 机制不同 2. 事件循环
  • 6.2 使用setTimeout模拟setInterval,有什么优势?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment