1.原型和原型链

实例化对象的proto属性指向被实例化对象的原型,prototype是一个对象,也有它自己的proto属性。
8ee9481ce0b1e7ea52f20c2a97c41a3.png
面试题例子:

  1. var F = function(){}
  2. Function.prototype.a = function(){}
  3. Object.prototype.b = function(){}
  4. var f = new F()

f.proto===F.prototype(此时F作为一个构造函数,才有prototype属性)
F.prototype.proto===Object.prototype(F.prototype意思是F这个函数的原型,函数原型的原型是Object,所以它的proto指向Object原型)

F.proto===Function.prototype(F相当于Function类型的一个实例化对象)
Function.prototype.proto===Object.prototype(所有类型都是Object的实例化对象)

综上,f对象的原型链上找不到Function.prototype,所以f对象不存在a方法,存在b方法。

2.call、apply和bind的实现

首先了解this的指向是如何变化的,有四种绑定方式:默认绑定、隐式绑定、硬绑定、构造函数绑定。默认情况下this指向window,对象调用里面的方法时this会绑定到该对象上,call、apply和bind可以进行硬绑定,把指定对象绑定到this上。

面试题例子:

  1. var name ='小红';
  2. function a(){
  3. var name = '小白';
  4. console.log(this.name);
  5. }
  6. function d(i){
  7. return i();
  8. }
  9. var b = {
  10. name:'小黄',
  11. detail:function(){
  12. console.log(this.name);
  13. },
  14. bibi:function(){
  15. return function(){
  16. console.log(this.name);
  17. }
  18. }
  19. }
  20. var c = b.detail;
  21. b.a = a;
  22. var e = b.bibi();
  23. a();
  24. c();
  25. b.a();
  26. d(b.detail);
  27. e();

输出结果为:小红、小红、小黄、小红、小红,这里只有当b调用自己的a方法时this才会绑定到b对象上,输出的是小黄,其他都是函数自身的调用,this都指向window,所以输出的是window下的name属性也就是小红。

call的实现:

  1. Function.prototype.newcall = function(obj,...rest){ //...rest是不定长参数
  2. obj = obj || window; //如果传入对象为null就指向window
  3. obj.p = this; //把被绑定对象的方法赋值给该对象
  4. var result = obj.p(...rest); //使用...解构rest数组
  5. delete obj.p; //调用完要删除,因为不能修改该对象
  6. return result;
  7. }

apply的实现:(与call类似)

  1. Function.prototype.newapply = function(obj,arr){ //apply第二个参数是数组
  2. obj = obj || window;
  3. var result;
  4. obj.p = this;
  5. if(!arr){ //如果第二个参数为空直接执行
  6. result = obj.p();
  7. }else{
  8. result = obj.p(...arr); //不为空则使用...解构
  9. }
  10. delete obj.p;
  11. return result;
  12. }

bind的实现:

  1. Function.prototype.newbind = function(obj,...rest){
  2. var that = this; //bind需要返回一个函数,所以预先保存this
  3. newf = function(args){
  4. obj = obj || window;
  5. var arr = rest.concat(args);
  6. if( this instanceof newf){ //如果使用了new,绑定该实例化对象
  7. this.p = that;
  8. this.p(...arr);
  9. delete this.p;
  10. }else{
  11. obj.p = that;
  12. obj.p(...arr);
  13. delete obj.p;
  14. }
  15. }
  16. newf.prototype = that.prototype; //使实例化对象与原函数关联起来
  17. return newf;
  18. }
  19. //new的使用范例:
  20. //var bibi = person.newbind(stu,1,2,3,4);
  21. //var b = new bibi(5);

3.var、let和const

let不能重复定义,const不允许重复定义和值的修改,但如果是引用类型,其引用的内容可以修改。
var会进行声明提升,可以先使用后声明,值是undefined。
let和const可以创建块级作用域。

4.函数柯里化

柯里化(Currying)是把接受多个参数的函数转变为接受一个单一参数的函数,并且返回接受余下的参数且返回结果的新函数的技术。

面试题例子:实现这个add函数
1649687452(1).png

  1. function add(){
  2. let args = [...arguments];
  3. let inner = function(){
  4. args.push(...arguments);
  5. return inner;
  6. }
  7. inner.toString = function(){
  8. return args.reduce((l,r) => l + r);
  9. }
  10. return inner;
  11. }

5.深拷贝和浅拷贝

对于基本数据类型,没有深浅拷贝之分。
对于引用数据类型,比如对象和数组,浅拷贝并没有进行真正的复制,副本仍指向原来的数据,若对副本进行修改会影响原来的数据。深拷贝是真正意义上的复制,副本与原来的数据完全独立,互不影响。

常见的浅拷贝方法:对象的Object.assign(),数组的concat()、slice()、Array.from()、reduce()、map()、filter(),…展开运算符。

深拷贝方法:JSON.parse(JSON.stringify())
弊端:1.Date对象变成字符串。2.RegExp、Error对象变为空。3.function和undefined丢失。4.NaN和Infinity变为null。5.丢失constructor属性。6.循环引用无法正确实现深拷贝。

手写深拷贝:

  1. function deepclone(obj){
  2. if(typeof obj !== 'object' || obj === null){
  3. return obj;
  4. }
  5. let result;
  6. if(obj instanceof Array){
  7. result = [];
  8. }else{
  9. result = {};
  10. }
  11. for(let key in obj){
  12. if(obj.hasOwnProperty(key)){
  13. result[key] = deepclone(obj[key]);
  14. }
  15. }
  16. return result;
  17. }

6.fetch方法

fetch返回的是一个Promise对象,所以可以使用Promise的then链式结构对结果进行处理。

get方法:

  1. fetch('').then(res => {
  2. if(res.ok){
  3. res.json();
  4. }
  5. throw new Error('');
  6. }).then(data => console.log(data.message));

post方法:

  1. fetch('',{
  2. method:'POST',
  3. body:JSON.stringify({name: input.value}),
  4. headers:{
  5. 'Content-Type':'application/json'
  6. }
  7. }).then(res => res.json()).then(data => console.log(data));

7.Promise

Promise是异步编程的一种解决方案,它是一个对象,可以获取异步操作的消息,简单来说就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作)的结果。他的出现大大改善了异步编程的困境,避免了地狱回调,Promise 提供统一的 API,各种异步操作都可以用同样的方法进行处理。

手写Promise:

  1. class Commitment{
  2. static PENDING = '等待';static FUFILLED = '成功';static REJECTED = '失败';
  3. constructor(fun){
  4. this.status = Commitment.PENDING;
  5. this.result = null;
  6. this.resolveCallback = [];
  7. this.rejectCallback = [];
  8. try{
  9. fun(this.resolve.bind(this),this.reject.bind(this));
  10. }catch(error){
  11. this.reject(error);
  12. }
  13. }
  14. resolve(result){
  15. setTimeout(() => {
  16. if(this.status === Commitment.PENDING){
  17. this.status = Commitment.FUFILLED;
  18. this.result = result;
  19. this.resolveCallback.forEach(callback =>{
  20. callback(result);
  21. })
  22. }
  23. });
  24. }
  25. reject(result){
  26. setTimeout(() => {
  27. if(this.status === Commitment.PENDING){
  28. this.status = Commitment.REJECTED;
  29. this.result = result;
  30. this.rejectCallback.forEach(callback =>{
  31. callback(result);
  32. })
  33. }
  34. });
  35. }
  36. then(onFulfilled,onRejected){
  37. return new Commitment((resolve,reject)=>{
  38. onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : ()=>{};
  39. onRejected = typeof onRejected === 'function' ? onRejected : ()=>{};
  40. if(this.status === Commitment.PENDING){
  41. this.resolveCallback.push(onFulfilled);
  42. this.rejectCallback.push(onRejected);
  43. }
  44. if(this.status === Commitment.FUFILLED){
  45. setTimeout(() => {
  46. onFulfilled(this.result);
  47. });
  48. }
  49. if(this.status === Commitment.REJECTED){
  50. setTimeout(() => {
  51. onRejected(this.result);
  52. });
  53. }
  54. })
  55. }
  56. }

Promise.all()参数是多个Promise实例,全部成功才返回一个成功的Promise
Promise.race()参数是多个Promise实例,返回最快改变状态值的Promise数据

8.Axios

基于Promise发送异步请求的库,通常有请求拦截器,一般设置token或其他后端需求,响应拦截器一般设置响应信息的状态码。

9.事件循环Event Loop

分为浏览器事件循环和Node.js事件循环,决定js代码的执行顺序。
同步任务在运行栈执行,异步任务放到任务队列。
宏任务:setTimeout、setInterval、I/O、UI交互事件、setImmediate(node.js环境)
微任务:promise回调、MutationObserver、Object.observe、process.nextTick(node环境)

10.JS异步编程方式

1.回调函数
2.promise
3.生成器函数generator和yield
4.async和await
5.事件监听
6.发布/订阅

11.持久化存储

1.localStorage:本地级存储,可将数据永久保存至硬盘中,关闭页面保存的数据不会消失,除非用户手动删除。
2.sessionStorage:会话级存储,可将数据临时保存至硬盘中,关闭页面保存的数据消失。
注:不同浏览器之间无法共享localStorage和sessionStorage,同一浏览器的不同页面可以共享localStorage,不同页面无法共享sessionStorage。
3.cookie