您的位置:

提高网站可读性:使用ES2017的新特性

一、使用异步函数

ES7引入了异步函数,也叫async/await函数。这个新特性可以让我们轻松地处理异步操作,使代码更加易读、易维护,提高网站可读性。

异步函数以async关键字开头,内部可以使用await来等待异步操作的完成。在使用异步函数时需要注意以下几点:

1、异步函数总是返回一个Promise对象。

2、await关键字只能在异步函数内部使用。

3、如果一个await表达式的Promise被拒绝了,那么await表达式会抛出异常,并且整个异步函数会停止执行。

async function getData() {
  try {
    const response = await fetch('/api/data');
    const data = await response.json();
    return data;
  } catch (error) {
    console.log(error);
  }
}

二、使用对象初始化简写

ES6引入了对象初始化简写,ES7又增加了一些新特性。使用对象初始化简写能够使代码更加简洁、易读。

1、可以使用变量名作为对象属性名,如果变量名和属性名相同,那么可以使用一次。

2、可以省略函数属性中的function关键字。

const name = 'John';
const age = 25;

const person = { name, age };

const calculator = {
  add(a, b) {
    return a + b;
  }
};

三、使用includes()方法

ES7引入了includes()方法,用来判断一个字符串是否包含另一个字符串。它与indexOf()方法的区别是,indexOf()方法返回的是位置,而includes()方法返回的是布尔值。

使用includes()方法可以更加直观地理解代码意图,提高网站可读性。

const str = 'hello world';

if (str.includes('hello')) {
  console.log('str包含hello');
}

四、使用平方和立方运算符

ES7引入了平方和立方运算符,可以将一个数字平方或立方。

使用这两个运算符可以使代码更加简洁、易读。

const square = 2 ** 2;

const cube = 3 ** 3;

五、使用尾逗号

ES7允许对象和数组的最后一个元素有尾逗号,这个特性可以让我们更加方便地添加、删除、移动数组或对象的元素,提高代码的可读性。

在使用尾逗号时需要注意以下几点:

1、IE8及以下浏览器不支持尾逗号。

2、如果使用了babel等工具自动编译代码,则需要设置相应的插件。

const arr = [
  1,
  2,
  3,
];

const obj = {
  name: 'John',
  age: 25,
};
提高网站可读性:使用ES2017的新特性

2023-05-20
使用web safe serif fonts提高网站字体的可

2023-05-12
提高网站可读性:用CSS更换字体

2023-05-12
印象笔记记录java学习(Java成长笔记)

2022-11-12
CSS控制字体样式:如何提高网站的可读性和视觉效果

2023-05-12
Mac笔记:在日常生活中高效实用的笔记工具

2023-05-18
如何使用text transform CSS提高网站可读性

2023-05-12
如何使用Array.some方法来提高网站内容的可读性和可用

2023-05-16
如何提高网站内容的可读性

2023-05-12
如何合理运用HTML中的span标签提高网站可读性

2023-05-17
使用Sass提高网站性能和可维护性

2023-05-17
提高网页可读性的技巧

2023-05-12
提高网页可读性的Python字体处理技巧

2023-05-12
mysql8数据库特性(mysql8 新特性)

2022-11-12
怎么抽取网页整理,怎么抽取网页整理数据

2023-01-08
java学习笔记(java初学笔记)

2022-11-14
when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: ${item.id} [in template "article/detail/index.ftl" at line 48, column 106] ---- Java stack trace (for programmers): ---- freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...] at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:134) at freemarker.core.EvalUtil.coerceModelToTextualCommon(EvalUtil.java:481) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:401) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:370) at freemarker.core.DollarVariable.calculateInterpolatedStringOrMarkup(DollarVariable.java:104) at freemarker.core.DollarVariable.accept(DollarVariable.java:63) at freemarker.core.Environment.visit(Environment.java:371) at freemarker.core.IteratorBlock$IterationContext.executedNestedContentForCollOrSeqListing(IteratorBlock.java:321) at freemarker.core.IteratorBlock$IterationContext.executeNestedContent(IteratorBlock.java:271) at freemarker.core.IteratorBlock$IterationContext.accept(IteratorBlock.java:244) at freemarker.core.Environment.visitIteratorBlock(Environment.java:645) at freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:108) at freemarker.core.IteratorBlock.accept(IteratorBlock.java:94) at freemarker.core.Environment.visit(Environment.java:335) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.process(Environment.java:314) at freemarker.template.Template.process(Template.java:383) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.processTemplate(FreeMarkerView.java:332) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.doRender(FreeMarkerView.java:266) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.renderMergedTemplateModel(FreeMarkerView.java:220) at org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:181) at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:314) at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1431) at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1167) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1106) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:205) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at com.software.filter.HttpSpiderIdentifyFilter.doFilter(HttpSpiderIdentifyFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:340) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) at java.base/java.lang.VirtualThread.run(VirtualThread.java:309)