search
HomeWeb Front-endJS TutorialConsolidation of basic front-end knowledge
Consolidation of basic front-end knowledgeJun 19, 2020 am 10:33 AM
javascript

Category to consolidate the basic knowledge of the front-end

It’s been a long time since I looked back at some basic knowledge of html and js. When I had nothing to do on the weekend, I saw an article about interview questions, and suddenly I discovered a lot I have all forgotten, okay, let’s strike while the iron is hot and learn some basic knowledge.

  • About HTML

html is the abbreviation of Hyper Text Markup Language (Hyper Text Markup Language), an application under the standard universal markup language. HTML is not a programming language, but a markup language, which is necessary for web page production. Can contain non-text elements such as pictures, music, videos, links, programs, etc.

  • The role of DOCTYPE

DOCTYPE is the abbreviation of document type. It is not an html tag. It is a document type of markup language. Statement, intended to tell the browser what version of the current HTML is written. Which specification (DTD) the browser should use to parse the document (such as HTML or XHTML specification).

  • What new tags have been added to HTML5 and their advantages

    Defining the page or area header
    Specifying the main content of the document
    Define the bottom of a page or area
    Define an article
  • About CSS

    css is a cascading style sheet (full English name: Cascading Style Sheets), which is a type of HTML (Standard Universal Markup Language) application) or XML (a subset of Standard Generalized Markup Language). CSS can not only statically modify web pages, but can also cooperate with various scripting languages ​​to dynamically format various elements of web pages.

  • What is the box model

The box model is also called the box model (Box Model), including element content (content), padding ( padding), border (border), margin (margin). Calculate the width and height of a box: content padding border.

  • Commonly used css units

1.px (represents pixels, which is an absolute unit. It will not change due to changes in the size of other elements. The page is displayed according to precise pixels)
2.pt (Points is an absolute unit and a physical unit of length, equal to 1/72 inch. Its value range is similar to px)
3.em (relative The font size of the text in the current object, for example, body's font-size: 12px; then for the child elements inside the body, 1em=12px)
4.rem (relative to the unit of the root element font size, it is also a Relative unit, somewhat similar to em, except that one calculation rule depends on the root element and the other on the parent element)
5.vh (1/100 of the height of the layout viewport)
6.vw (the height of the layout viewport 1/100)

  • Clear floating

When do you need to clear floating and what are the methods to clear floating?
When the float attribute is set on the child element, and the parent element does not set the width and height, and is supported by the child element, it will cause the width and height of the parent element to collapse. At this time, the float needs to be cleared.
Method: 1. Set the attribute overflow:hidden on the parent element || auto

 2、子元素最后一个元素添加clear:both
 3、父级定义伪类:after和zoom 
 {display:block;clear:both;content:"",visibility:hidden;height:0}
 {zoom:1}
 4、父级定义height
 5、父级也一起浮动
  • How to keep an element centered horizontally and vertically

1 , transform
.children{

position:absolute;
top:50%;
left:50%;
-webkit-transform: translate(-50%,-50%)

}
2.flexbox
.parent{

display:flex;
justify-content:center;
align-items: center;

}
3. When the width and height of the child element are fixed, the parent element When the width and height are not 0,
.parent{

height:200px;

}
.children{

width:100px;
height:100px;
position:absolute;
top:50%;
left:50%;
margin: -50px 0 0 -50px;

}

  • css selector has Which and what is the priority

1. Tag selector (body, p, etc.)
2. Class selector (class="box")
3. ID selector (id="app")
4, global selector (*{})
5, combined selector (.header .header_box)
6, descendant selector (.#header ul li)
7. Group selector (.box .header .content)
8. Inherited selector (p p)
9. Pseudo-class selector (link visited active hover)
10. Attribute selectors for string matching (^ $ * correspond to the beginning, end, and inclusion respectively)
11, sub-selector (p>p)
12, css adjacent sibling selector (p p)

Priority:
!important > Inline Style> ID Selector> Class Selector> Tag Selector> Wildcard> Inheritance> Browser Default Properties

每个选择器的权值:
行内样式 1000
ID选择器 100
class选择器 10
HTML标签选择器 1

  • sass

sass: 是一款强化css的辅助工具,它在css语法的基础上变量、嵌套、混合、导入等高级功能,可提高开发效率。
sass有两种语法格式,首先是scss,是在css3的语法基础上进行拓展,所有css3语法在scss中都是通用的,同时加入sass的特色功能。此外,scss也支持大多数css hacks写法以及浏览器前缀写法,以及早期的IE滤镜写法,这种格式以.scss作为拓展名。
另一种也是最早的sass语法格式,被称为缩进格式(Indented Sass)通常简称“sass”,只是与scss相比个别地方采取了不同的表达方式。
使用:
定义变量($width:5rem #box{width:$width})
混合器 (@Minxin class {

border-radius:5px;
background:#f00;

}
.box{

@include class

}
具体更多用法参考文档sass官方文档。

  • javascript基础

1、js有哪些数据类型:

原始数据类型 - String Number Boolean Undefined null
对象类型 - Array Function Object

null和undefined的区别 - undefined是一个特殊值,通常不是变量尚未赋值。null是在计算机中具有保留的值,用于指示指针不引用有效对象。可以把undefined作为空的变量,null看做是空的对象。
原始类型和引用类型的区别 - 基本类型指的是保存在栈内存中的简单数据段,
引用类型指的是那些保存在堆内存中的对象,意思是,变量中保存的实际上是只是一个指针,这个指针执行内存中的另一个位置,由该位置保存对象。
访问的方式也不同 - 基本类型是按值访问,操作也是他们的实际保存的值,
引用类型是按引用方式访问,当查询时,我们需要先从栈中读取内存地址,然后再找到保存在堆内存中的值。
复制的类型也不同 - 基本类型变量的复制,从一个变量向另一个变量复制时,会在栈中创建一个新值,然后把值复制到新变量分配的位置上。
引用类型复制,复制的是存储在栈中的指针,将指针复制到栈中为新变量分配的空间中,而这个指针副本和原指针执行存储在堆中的同一个对象;复制结束后,两个变量实际上引用同一个对象,因此改变其中一个,讲影响另一个。

2、如何理解prototype,什么时候用prototype
在javascript中,所有的对象都是Object的实例,并且会继承Object.prototype的属性和方法,那么,Object.prototype是所有对象的爸爸。我们在创建对象时,就会有一些已经定义好的属性,在定义函数时候,这个预定义的属性就是prototype,是函数的一个属性,是一个指向对象的指针。而定义一个普通对象时候,会生成一个__proto__,指向的是对象的构造函数prototype。
js中构造函数和实例之间有着微妙的关系,构造函数通过定义prototype来约定其实例的规格,再通过new来构造实例,他们的作用就是产生对象。通过new构造对象的特点是,obj的prototype指向了构造器的prototype属性。
什么时候用prototype呢?
使用prototype的好处是不会产生额外的内存,所有实例化后的对象都会从原型上继承这个方法,相当于公共方法,就是一个子类拥有父类的某些特性,又可以给自己添加一些独立的属性,而且不会影响父时使用prototype。
prototype通常来解决一个问题:对象的创建比较耗费资源,例如,对象创建的时候需要访问数据库、读取外部文件、需要使用网络。这些都是比较耗费时间和内存的。
写一个prototype小栗子:

            function Parent(name){
                this.drink = function(){
                    return 'I Can Drink\t' + name
                }
            }
            let parent = new Parent('red wine')
            console.log(parent.drink())//I Can Drink red wine

            function Person (name, age) {
                Parent.call(this,'beer')
                this.name = name;
                this.age = age;
            }
            Person.prototype.sayName = function() {
                return this.name
            }

            let person =  new Person('sunny', 18)
            let person1 = new Person('cherry',28)
            console.log(person.sayName()) // sunny
            console.log(person1.drink())// I Can Drink beer

3.什么是闭包、使用场景有哪些、闭包会引起什么问题
概念:闭包就是一种可以重复使用变量而且不会造成全局变量污染的机制,它是一种特殊的对象。
它由两部分组成:函数以及创建该函数的环境。而环境是由闭包创建时在作用域中的任何局部变量组成。可以用来定义私有属性和方法。
小栗子:

function sum(){
    let x = 1,y = 2;
    function res(){ //闭包
        return x + y
    }
    return res //闭包函数
}
let result = sum();
result() // 3

从上面的这个简单的闭包例子可以看出,在外部函数的内部创建一个内部函数。并且内部函数读取着外部函数作用域中定义的局部变量,而内部函数又作为外部函数的返回值。所以总结一下:闭包的作用域链包含着它自己的作用域,以及包含它的函数的作用域和全局作用域。
使用场景:需要设计私有变量和方法的时候。
举栗子:

function Person() {
    let name = 'sunny'
    this.getName = () => name
}
let person = new Person;
person.name //undefined
person.getName() // sunny

闭包引起的问题:正常情况下,一个函数的作用域和其所有变量在函数执行后都会被销毁,但是在创建一个闭包后,这个函数会一直保存,直到闭包不存在为止。也就是闭包函数会一直保存着父函数的执行期上下文。这样就会造成内存泄露。
举个栗子:

const handleDom = () => {
    let dom = document.getElementById('#el');
    let id = dom.id;
    dom.onclick = () => {
        console.log(id)
    }
    dom = null;
}

上面这个函数中的闭包函数里保存着一个HTML元素,这就意味着这个元素无法销毁,所以我们应该在操作完元素之后要主动销毁。
还有一个场景是缓存机制,栗子在下面:

(() => {
    let num = 0;
    setInterval(() => {
        console.log(num++);
    }, 1000)
})() // 1 2 3 4 5 6 ....

这个就验证了是吗的说法:闭包函数会一直拿着外部函数的变量,并且不会销毁,所以会一直用着加1之后的最新值。

4.什么是浅拷贝和深拷贝?怎么实现一个深拷贝?
在这之前要先理解一下js的基本类型和引用类型的区别:
基本类型:直接存储在栈中的数据(String、Number、Boolean、Undefined、Null)
引用类型:是把该对象的引用地址存储在栈中,对象里的数据放在堆中(Array、Function、Object)
浅拷贝:只拷贝一层,不会拷贝对象的子元素,相当于直接赋值。
深拷贝:会克隆出一个对象,数据是相同的,但是引用地址不同。
举栗子说明:

let person = {
    name:'sunny',
    age:13,
    sayName:function(){
        console.log(this.name)
    }
}
let person1 = person;
console.log(person1) //和person的相同
person1.name = 'cherry'
console.log(person1.name) //cherry
console.log(person.name) //cherry

因为是引用的用一个地址,所以person1的改变也修改person的值;

深拷贝:
es6的 "..."可以实现一个深拷贝,其实也说不上深拷贝, 如果对象里面包含引用类型,也会失败。

let obj = {
    name:'sunny',
    age: 30,
    arr: [1,2,3,4]
}
let obj1 = obj
// console.log(obj1)
// obj1.name = 'cherry'
// console.log(obj1)
let copyObj = { ...obj }
copyObj.name = 'cherry'
copyObj.arr[0] = 8
console.log(copyObj)

console.log(obj)//obj的arr被改变了 ,但是基本类型没有受到影响

深拷贝实例:

const deepClone = (val) => {
  let res;
  if (val && Array.isArray(val)) {
    res = [];
    val.forEach((item) => {
      res.push(deepClone(item));
    });
  } else if (typeof val === 'object' && val !== null) {
    res = {};
    for (let key of Object.keys(val)) {
      res[key] = deepClone(val[key]);
    }
  } else {
    res = val;
  }
  return res;
}

推荐教程:《JS教程

The above is the detailed content of Consolidation of basic front-end knowledge. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript的Symbol类型、隐藏属性及全局注册表详解JavaScript的Symbol类型、隐藏属性及全局注册表详解Jun 02, 2022 am 11:50 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

JavaScript面向对象详细解析之属性描述符JavaScript面向对象详细解析之属性描述符May 27, 2022 pm 05:29 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

整理总结JavaScript常见的BOM操作整理总结JavaScript常见的BOM操作Jun 01, 2022 am 11:43 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach是es6里的吗foreach是es6里的吗May 05, 2022 pm 05:59 PM

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor