Vue 2019 front-end interview, foundation

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_42565994/article/details/102514255

Disclaimer:
Author: front-end small sauce
https://segmentfault.com/a/1190000018225708

After graduation in Hefei been a small company, no old driver, no technical atmosphere, on the road I can only grope technology alone. The boss will only castles in the air, the future of a confused do not see any hope. Ever since, I determined resignation, came to Hangzhou to start in the New Year on the occasion, here's Internet companies should be Hefei few times now. . . .

When I first came three days interviewing several companies, some relatively small size, some are start-up companies, some already developed good; today the recent interview questions to be aggregated, but also to their complex disks, due to my technical stack mainly Vue, so most of the questions are related to the development of Vue.

1. Talk about your understanding of MVVM development model

MVVM is divided into Model, View, ViewModel three.

  • Model: Representative data model, data and business logic layer is defined in the Model;
  • View: on behalf of UI view, responsible for displaying data;
  • ViewModel: Model change is responsible for monitoring and controlling the data in the view update processing user interaction;

Model and View are not directly related, but rather through contact ViewModel, has a two-way data binding link between Model and ViewModel. Therefore, when the data changes in the Model layer will trigger a refresh View, View the result of user interaction changed data will be synchronized in the Model.

This mode automatically to achieve the Model and View data synchronization, so developers only need to focus on the maintenance operations to the data, without the need for their own operations dom.

2. Vue which instructions have?

v-html、v-show、v-if、v-for等等。

3. v-if and v-show What's the difference?

v-show only control the display elements, the display property toggle block and none; and v-if the DOM node controls the presence or absence. When we need to switch often an element of show / hide, using the v-show cost savings will be more on performance; when only one show or hide, using the v-if more reasonable.

4. Brief responsive principle of Vue

When an instance is created Vue, vue will traverse attribute data options, with Object.defineProperty them into getter / setter and tracking of dependencies inside, notice the change when the property is accessed and modified. Each component instance has a corresponding watcher program example, it is recorded as attribute dependencies in the assembly process of rendering, when the dependency after the setter is invoked, it informs the watcher recalculated, thereby causing its associated components to be updated .

Here Insert Picture Description

5. Vue how to achieve a two-way data binding in internal components?

Suppose there is an input box assembly, the user input component synchronization data parent page.

Specific ideas: to a parent element subassembly, the subassembly value notified by props pass through $ EMIT modify the parent component values ​​props, specifically implemented as follows:

Here Insert Picture Description
Here Insert Picture Description
We did two things in the parent components, sub-assemblies to pass first, props, and second, listen for input events and sync their property value. Then this two-step operation can further simplify a little bit? The answer is yes, you only need to modify the parent component:
Here Insert Picture Description
v-Model will actually help us complete the above two steps.

6. Vue how to monitor changes in the value of a property?

For example, now we need to monitor data, the change of obj.a. Vue monitor changes in object properties you can do:
Here Insert Picture Description
Deep attribute indicates deep traversal, but arguably will monitor all changes in the properties of obj, not what we want, so do some modifications:
Here Insert Picture Description
there is a way, can be computed by achieved only:
Here Insert Picture Description
using the calculated characteristic properties is achieved when dependent changes, it will re-calculate a new value.

What happens when 7. Vue add a new attribute to the attribute data of the object, how to solve?

示例:
Here Insert Picture Description
Here Insert Picture Description
原因在于在Vue实例创建时, obj.b 并未声明,因此就没有被Vue转换为响应式的属性,自然就不会触发视图的更新,这时就需要使用Vue的全局api—— $set():

$set() 方法相当于手动的去把 obj.b 处理成一个响应式的属性,此时视图也会跟着改变了:

8. delete和Vue.delete删除数组的区别

delete只是被删除的元素变成了 empty/undefined 其他的元素的键值还是不变。

Vue.delete 直接删除了数组 改变了数组的键值。

9.如何优化SPA应用的首屏加载速度慢的问题?

  • 将公用的JS库通过script标签外部引入,减小 app.bundel 的大小,让浏览器并行下载资源文件,提高下载速度;
  • 在配置 路由时,页面和组件使用懒加载的方式引入,进一步缩小 app.bundel 的体积,在调用某个组件时再加载对应的js文件;
  • 加一个首屏loading图,提升用户体验;

10. 前端如何优化网站性能?

1、减少 HTTP 请求数量

在浏览器与服务器进行通信时,主要是通过 HTTP 进行通信。浏览器与服务器需要经过三次握手,每次握手需要花费大量时间。而且不同浏览器对资源文件并发请求数量有限(不同浏览器允许并发数),一旦 HTTP 请求数量达到一定数量,资源请求就存在等待状态,这是很致命的,因此减少 HTTP 的请求数量可以很大程度上对网站性能进行优化。

CSS Sprites

国内俗称CSS精灵,这是将多张图片合并成一张图片达到减少HTTP请求的一种解决方案,可以通过CSS的background属性来访问图片内容。这种方案同时还可以减少图片总字节数。

合并 CSS 和 JS 文件
现在前端有很多工程化打包工具,如:grunt、gulp、webpack等。为了减少 HTTP 请求数量,可以通过这些工具再发布前将多个CSS或者多个JS合并成一个文件。

采用 lazyLoad
俗称懒加载,可以控制网页上的内容在一开始无需加载,不需要发请求,等到用户操作真正需要的时候立即加载出内容。这样就控制了网页资源一次性请求数量。

2、控制资源文件加载优先级

浏览器在加载HTML内容时,是将HTML内容从上至下依次解析,解析到link或者script标签就会加载href或者src对应链接内容,为了第一时间展示页面给用户,就需要将CSS提前加载,不要受 JS 加载影响。

一般情况下都是CSS在头部,JS在底部。

3、利用浏览器缓存

浏览器缓存是将网络资源存储在本地,等待下次请求该资源时,如果资源已经存在就不需要到服务器重新请求该资源,直接在本地读取该资源。

4、减少重排(Reflow)

基本原理:重排是DOM的变化影响到了元素的几何属性(宽和高),浏览器会重新计算元素的几何属性,会使渲染树中受到影响的部分失效,浏览器会验证 DOM 树上的所有其它结点的visibility属性,这也是Reflow低效的原因。如果Reflow的过于频繁,CPU使用率就会急剧上升。

减少Reflow,如果需要在DOM操作时添加样式,尽量使用 增加class属性,而不是通过style操作样式。

5、减少 DOM 操作

6、图标使用 IconFont 替换

11. 网页从输入网址到渲染完成经历了哪些过程?

大致可以分为如下7步:

  1. 输入网址;
  2. 发送到DNS服务器,并获取域名对应的web服务器对应的ip地址;
  3. 与web服务器建立TCP连接;
  4. 浏览器向web服务器发送http请求;
  5. web服务器响应请求,并返回指定url的数据(或错误信息,或重定向的新的url地址);
  6. 浏览器下载web服务器返回的数据及解析html源文件;
  7. 生成DOM树,解析css和js,渲染页面,直至显示完成;

12. jQuery获取的dom对象和原生的dom对象有何区别?

js原生获取的dom是一个对象,jQuery对象就是一个数组对象,其实就是选择出来的元素的数组集合,所以说他们两者是不同的对象类型不等价。

Here Insert Picture Description

13. How to extend custom methods jQuery

Here Insert Picture Description
For now the company interview questions is quite basic, but for some only the pursuit of research will not use the principle of students who may not be so easy. Therefore, we must not only pursue the breadth of learning, but also to the pursuit of depth.

Refer to "Talking about the performance of the website front-end performance optimization"

Guess you like

Origin blog.csdn.net/qq_42565994/article/details/102514255