8.21 字节跳动前端面试重要问题(待补充)
1.手写bind()
Function.prototype.myBind = function(context){
if(typeof this !== "function"){ throw new Error("not a function")
const self = context;
let args = [...arguments].slice(1);//截取掉第一个this,保留其他的参数
return function(){
self.apply(this, args);
}
}手写call
Function.prototype.myCall = function(context){
var context = Object(context) || window //判断有没有传入的对象,没有则默认是window
context.fn = this;//在对象上新建一个属性,属性值为函数
let result = "";
const args = [...arguments].slice(1);
result = context.fn(...args);
delete context.fn;//删除该属性
return result;
}2.CSS ul下的第二个li
ul>li:nth-child(2){
}