每次的分割代表一次面试~
<img>
的 title 和 alt 有什么区别?function getUrlParam(url, key){
};
function minDistance(word1, word2){
};
function fn(a) {
console.log(a);
var a = 2;
function a() {};
console.log(a);
}
fn(1);
var a = 10;
a.pro = 10;
console.log(a.pro + a);
var s = 'hello';
s.pro = 'world';
console.log(s.pro + s);
console.log(typeof null);
console.log(typeof {});
console.log(typeof []);
console.log(typeof undefined);
function printing() {
console.log(1);
setTimeout(function() {
console.log(2);
}, 1000);
setTimeout(function() {
console.log(3);
}, 0);
console.log(4);
}
printing();
var numberArray = [3, 6, 2, 4, 1, 5];
1
以下写法均为个人解答,不代表正确性,有兴趣可以自己写写~
function getString (string) {
const Reg = /^[a-zA-Z]*$/
const newString = []
const stringArr = string.split('')
let preString = ''
for (const str of stringArr) {
if (Reg.test(str) || str === "'") {
if (preString === '?') {
newString.push(str.toUpperCase())
} else {
newString.push(str)
}
}
preString = str
}
return newString.join('')
}
const string = `I'm????driving????to??beijing????after?breakfast`
console.log(getString(string))
const numberList = []
function getNumberList (start, end) {
start = parseInt(start)
end = parseInt(end)
if (start <= end) {
if (start > 0 && start % 3 === 0) {
numberList.push(start)
getNumberList(start + 3, end)
} else {
getNumberList(++start, end)
}
}
}
getNumberList(1, 100)
console.log(numberList)
let startDay = {
hour: 1,
timer: null,
setup (user) {
console.log(`${user} 起床啦`)
},
brush (user) {
console.log(`${user} 去刷牙呢`)
},
work (user) {
console.log(`${user} 去工作啦`)
},
sleep (user) {
console.log(`${user} 要睡觉啦`)
},
coding (user) {
console.log(`${user} 开始写代码啦`)
},
demand (user) {
console.log(`${user} 的需求评审`)
},
daily (user) {
console.log(`${user} 写日报`)
},
start () {
if (this.hour >= 1 && this.hour < 24) {
switch (this.hour) {
case 8:
execute(this.hour)
break
case 9:
execute(this.hour)
break
case 10:
execute(this.hour)
break
case 11:
execute(this.hour)
break
case 16:
execute(this.hour)
break
case 22:
execute(this.hour)
break
default:
break
}
} else {
this.hour = 0
}
},
open () {
console.log('开始了')
this.timer = setInterval(() => {
console.log(this.hour + '点')
this.start()
this.hour += 1
}, 1000)
},
close () {
clearInterval(this.timer)
this.timer = null
console.log('stop')
}
}
const config = {
my: {
8: startDay.setup,
9: startDay.brush,
10: startDay.work,
22: startDay.sleep
},
a: {
10: startDay.coding,
16: startDay.demand,
22: startDay.daily
},
b: {
11: startDay.coding,
23: startDay.daily
}
}
function execute (hour) {
config.my[hour] && config.my[hour]('我')
config.a[hour] && config.a[hour]('小 a')
config.b[hour] && config.b[hour]('小 b')
}
startDay.open()
input: [1, 2, 3]
output: [1, 2, 4]
input: [9, 9]
output: [1, 0, 0]
this.$store.state
去修改吗?input: abcedde
output: edde
input: abbccdddaa
output: adddaa
const a = function () {
const x = {
m: function () {
console.log(this)
},
n: () => {
console.log(this)
}
}
return x
}
const b = {}
const c = a.call(b)
c.m()
c.n()
const Car = function () {}
const car = new Car
const car1 = New(Car)
// 实现 New
Array.prototype.customReduce = function () {
}
用 Javascript 对象模拟 DOM 树,并实现它的 render 方法,通过调用该方法可以转成真正的 DOM 节点。例如我们已经实现了 element.js,通过 require('element.js'),我们可以定义 ul,如下:
const el = require('./element.js');
const ul = el('ul', {id: 'list'}, [
el('li', {class: 'item'}, ['Item 1']),
el('li', {class: 'item'}, ['Item 2']),
el('li', {class: 'item'}, ['Item 3'])
])
现在 ul 只是一个 JavaScript 对象表示的 DOM 结构,页面上并没有这个结构。我们可以根据这个 ul 构建真正的 <ul>
,最终当我们这样调用时,
const ulRoot = ul.render();
document.body.appendChild(ulRoot);
body
中就有了真正的 DOM 结构,如下
<body>
<ul id='list'>
<li class='item'>Item 1</li>
<li class='item'>Item 2</li>
<li class='item'>Item 3</li>
</ul>
</body>
也就是说,我们需要实现 element.js
点击查看代码
function el (tag, data, el) {
return {
render: function () {
const element = document.createElement(tag)
element.el = el
if (data) {
for (let key in data) {
switch (key) {
case 'id':
case 'class':
element.setAttribute(key, data[key])
break
default: break
}
}
}
for (let i = 0; i < el.length; i++) {
if (el[i].render) {
element.append(el[i].render())
} else {
element.append(el[i])
}
}
return element
}
}
}
Javascript 构造一个双向链表,并且实现它的插入和删除方法,例如 insert(position, element) 表示在 position 位置插入值为 element 的节点,removeAt(position) 表示删除 position 位置的节点。
// 链表节点
class Node {
constructor(element) {
this.element = element
this.prev = null
this.next = null
}
}
例如输入 15, 结果: [[1, 2, 3, 4, 5], [4, 5, 6], [7, 8]]
// let obj = {
// a: {
// b: {
// c: {
// d: 1,
// },
// },
// },
// e: 2,
// f: {
// g: 3,
// },
// };
// result
// [[a, e, f], [b, g], [c], [d]]
1.curry 实现
const add = (a, b, c) => a + b + c;
const curryAdd = curry(add);
console.log(curryAdd(1)(2)(3)); // 6
console.log(curryAdd(1, 2)(3)); // 6
console.log(curryAdd(1)(2, 3)); // 6
// 实现 B 继承 A
function A(t) {
this.t = t;
}
function B(t, s) {}
3.输出
function fn(a) {
console.log(a);
var a = 123;
console.log(a);
function a() {}
console.log(a);
var b = function() {};
console.log(b);
}
fn(1);