当前位置:网站首页>JS advanced -es6 syntax
JS advanced -es6 syntax
2022-06-30 02:15:00 【Boy 12】
ES6 Introduce
es6 type js Language next generation standard , Already in 2015 year 6 It was officially released in January, also known as es2015. Its goal , Is making JavaScript Language can be used to write complex large-scale applications , Become an enterprise development language .
let Command and const command
Key applications and concepts
var and let The difference between
1.var Can improve , Not affected by block level scope , Can repeat the definition of .
2.let You can't raise variables , Affected by block level scope , Cannot be defined repeatedly .
let and const The difference between
The only difference between them is const You must assign values when declaring , And cannot be reassigned
Block level scope
The block level scope is es6 Begin to put forward the concept ,es6 A block level scope can be formed by explicitly declaring braces in .
let and const Similarities
1.let and const Can not be variable promotion
console.log(arr);// Report errors
let arr;
console.log(concc);// Report errors
const concc=123;
2.let and const Are affected by the block level scope
//let
for(let i=0;i<5;i++){
setTimeout(function(){
console.log('i',i);
})
}
//const
{
const arr=0}
console.log(arr);// Report an error and say no statement
Deconstruction and assignment of variables
Knowledge point :
1. deconstruction : Structural decomposition ,s Decompose a part from a whole variable to use
2. An array of deconstruction
Make the elements in one array correspond to the elements in another array one by one
// Array structure
let arr=[1,2,3,4]
let [a,b,c,d]=arr;
console.log(" Deconstruction in arrays ",a);
3. Object to deconstruct
Because objects are unordered , So we can't use the idea of array deconstruction to see , We get the attribute value by binding the corresponding attribute name
// Object to deconstruct
let obj={
username : "lisi",
age : 20
}
let {
username ,age}=obj;
console.log(" I am object deconstruction ",username);
4. Function parameter deconstruction and default values
stay es6 Inside , We can set default values for formal parameters , When no arguments are passed in , You can make it equal to the default value
// Function deconstruction
function test({
username=" Small make up ",miao=" Gaga Shuai "}={
}){
console.log(username+miao);
}
test()
Object extension
1. A concise representation of properties and methods
let username=' Small make up '
let obj={
username ,// When the attribute name and attribute value are the same , It can be abbreviated as
say(){
console.log(this.username+" Gaga Shuai ");// Short for simple method
}
}
obj.say()
2. Variable as property name
let key ='username';
let obj={
[key] : ' Small make up ',
say(){
console.log(this[[key]]+" Gaga Shuai ");
}
}
obj.say();
3. Merge objects
Method : Object.assign
let obj1={
username : ' Li Si ',
say(){
console.log(this.username+" Gaga Shuai ");
}
}
let obj2={
username : ' Small make up '
}
let obj=Object.assign(obj1,obj2)// If there are duplicate attributes, the attribute values in the following attributes will overwrite the above
obj.say()
Arrow function
1. Common expressions for arrow functions
let test=()=>{
}// This is the arrow function
2. Arrowhead function this Point to
As mentioned earlier, arrow functions are not bound this, He will find the context this For your own use
let test={
say:()=>{
console.log(this," Xiaobian gagashuai ");//this Point to the window, But it is obj The function called
}
}
test.say()
3. Arrow functions cannot be constructors
The arrow function itself doesn't have this, So it can't be used as a constructor
var test=(name ,miao)=>{
this.name=name;
this.miao=miao;
}
let obj=new test(' Small make up ',' Gaga Shuai ');// Direct error
console.log(obj);
4. Arrowhead function arguments Object is not available
arguments Is a list of arguments
var test=(name ,miao)=>{
console.log(arguments);// Report errors , There is no such thing
this.name=name;
this.miao=miao;
}
test(1,2);
Promise( recite )
Knowledge point :
promise Definition
promise Use steps
async await Asynchronous to synchronous
promise Is a solution to asynchronous programming , Can be used to solve the problem of callback hell
(1) What is? promise
promise Promise in Chinese
promise There are three states :
pending In progress
resolved success
rejected Failure
Once the status changes , Can't change the state again , This is also its name promise- promise The origin of , One promise Object state can only be changed once
We can use promise To save future data
(2) promise How to use
establish promise object
Store successful or failed data
obtain promise Object's data
<!DOCTYPE html>
<html lang="en">
<body>
<script type="module"> let num = window.prompt(" Please enter a number "); // 1. establish promise object const obj = new Promise((resolve, reject) => {
// num > 10 Positioning successful , Otherwise failure if (num > 10) {
// 2. Used when successful resolve Save the data resolve({
msg:'sucess',num}); } else {
// 3. Fail with reject Save the data reject({
msg: 'error', num }); } }); // 3. get data then() Get successful data , catch() Get failed data obj.then((res)=> {
console.log(' success ', res); }).catch(err=> {
console.log(' Failure ',err); }) </script>
</body>
</html>
es6 modularization
//index.js
export default {
username : ' Small make up ',
say(){
console.log(this.username+" Gaga Shuai ");
}
}
export let username =" Small make up "
//index.html
<script type="module"> import * as obj from "./index.js";//as The name console.log(obj); </script>
边栏推荐
- scp远程拷贝命令记录
- Electron FAQ 54 - make your own fireworks based on electron
- 網上炒股安全麼?炒股需要開戶嗎?
- 什么是幂等性?四种接口幂等性方案详解!
- dhu编程练习
- True love forever valentine's Day gifts
- Add a second network card (network interface NIC) for the virtual machine in azure portal in 2 minutes
- DDoS "fire drill" service urges companies to prepare
- [protection mode] segment descriptor
- 210. Schedule II - depth traversal
猜你喜欢

选择排序

AI landing manufacturing: intelligent robots should have these four abilities

A keepalived high availability accident made me learn it again!

004_ icon

8 — router

CTF introductory learning (WEB direction)

DMX configuration

【MySQL 04】使用MySQL Workbench 8.0 CE 备份及恢复Linux中的MySQL数据库

有流量,但没有销售?增加网站销量的 6 个步骤

如何使用SMS向客户传递服务信息?指南在这里!
随机推荐
[MySQL 04] use MySQL workbench 8.0 CE to back up and restore MySQL databases in Linux
DDoS threat situation gets worse
Jenkins continuous integration environment construction VII (Jenkins parametric construction)
The odoo15 page jumps directly to the editing status
210. Schedule II - depth traversal
DHU programming exercise
Que se passe - t - il si un faux divorce devient un vrai divorce?
DHU programming exercise
dhu编程练习
Tencent released the first Office Photo 23 years ago. It's so chronological
widget使用setImageViewBitmap方法设置bug分析
Vs realize quick replacement function
DDoS extortion attacks: what you need to know
网上炒股安全么?炒股需要开户吗?
Jenkins continuous integration environment build 8 (configure mailbox server to send build results)
如何制作CSR(Certificate Signing Request)文件?
Comprendre le principe AQS (organigramme et schéma de file d'attente synchrone)
DHU programming exercise
图解 Google V8 # 19 :异步编程(二):V8 是如何实现 async/await 的?
ROS Bridge 笔记(01)— apt 安装、源码编译安装、安装依赖、运行显示