js advanced interview questions

js advanced classic interview questions (to be updated)

what does the new keyword do
1. 开辟一个内存存放新创建的对象--创建实例对象
2. 将构造函数里的this指向实例对象
3. 给实例对象新增属性和方法
4. 隐式的返回了实例对象
The difference between deep copy and shallow copy
Shallow copy refers to the creation of new data that has an exact copy of the original data attribute values
如果属性是基本类型,拷贝的就是基本类型的值。如果属性是引用类型,拷贝的就是内存地址

在JavaScript中,存在浅拷贝的现象有:

1. Object.assign
2. Array.prototype.slice()
3. Array.prototype.concat()
4. 使用拓展运算符实现的复制
Deep copy creates a new stack. The properties of the two objects are the same, but they correspond to two different addresses. Modifying the properties of one object will not change the properties of the other object.
常见的深拷贝方式有:

1. 递归 for-in遍历
2. JSON.parse(JSON.stringify())
3. jQuery  $.extend
4. lodash  _.cloneDeep()
Talking about the understanding of this pointing
1. 普通函数中:this → window
2. 普通对象里的方法(函数):  对象本身
3. 定时器中: this → window
4. 构造函数中: this → 当前实例化的对象
5. 事件处理函数中:this → 事件的触发对象
Closure understanding
闭包就是函数中包含另一个函数,可以让你在函数外部读取到内部的变量(就是在函数内部再定义一个函数),让这些变量的值始终保持在内存中,可以达到延长变量生命周期的效果,过多使用会导致内存泄漏的问题
prototype chain
Javascript 是面向对象的,每个实例对象都有一个__proto_属性,该属性指向它原型对象,这个实例对象的构造函数有一个原型属性 prototype,与实例的 __proto__属性指向同一个对象。当一个对象在查找一个属性的时,自身没有就会根据__proto__ 向它的原型进行查找,如果都没有,则向它的原型的原型继续查找,直到查到 Object.prototype._proto_为 nul,这样也就形成了原型链。
The difference between bind, call, and apply
1.call和apply会调用函数,且会改变函数内部的this指向
2.call和apply传递的参数不一样,call 的参数是直接放进去的,第二第三第 n 个参数全都用逗号分隔。而 apply 的所有参数都必须放在一个数组里面传进去。
3.bind 不会调用函数,可以改变函数内部指向,返回是函数,它的参数和 call 一样。
Stabilization and throttling
防抖: n 秒后在执行该事件,若在 n 秒内被重复触发,则重新计时
节流: n 秒内只运行一次,若在 n 秒内重复触发,只有一次生效

Guess you like

Origin blog.csdn.net/qq_45372417/article/details/126824597