class自定义类
class Fn{
//相当于构造函数
constructor(num){
this.x = num;
}
//给当前Fn类的原型上增加方法
say(){
console.log(1);
}
//给当前类的实例增加私有属性
x = 800;
//把Fn当成对象给Fn增加私有属性
static s = 200;
}
//但是class无法给Fn的原型增加键值对,只能在外面单独写了
//如果当前的类是用class创建的,那只能当做构造函数执行,不能当做普通函数执行
let f = new Fn();
console.log(f);
模板字符串
let s = 100;
// let str = '<span>' + s + '</span>'
let str = `<span>${s}</span>` // 模板字符串
call的面试题
function fn1() {console.log(1)}
function fn2() {console.log(2)}
fn1.call(fn2); //=> 1
fn1.call.call(fn2); //=> 2
Function.prototype.call(fn1); //=> 不输出
Function.prototype.call.call(fn1); //=> 1
规律: 如果出现两个及两个以上的call,最后就会让传进去的实参执行
总结: call 执行的时候,只需要弄清楚,哪个函数需要执行,函数执行的时候需要把里面的this改成谁。弄清这两个点就可以很容易的理解这种题目。
还有就是 call 函数内部的 this 指向的是需要立即执行的那个函数。这样就可以很容易得到答案了。
类数组转数组的方法
1.最low的方法
function fn(){
let ary = [];
for(var i = 0;i<arguments.length;i++){
ary.push(arguments[i])
}
return ary;
};
console.log(fn(1,2,3,4,5));
利用for循环和数组的push方法,这样就把类数组arguments转化为了数组
function fn(){
let ary = [];
return ary.slice.call(arguments,0);
};
console.log(fn(1,2,3,4,5));
找到Array类的原型上的slice方法,使用call改变slice的this指向,变为arguments
function fn(...ary){
return ary;
};
console.log(fn(1,2,3,4,5));
用剩余运算符 ...ary,放在形参的位置,接收实参,ary就变成了一个含有实参的数组