博客
关于我
代码的括号(前有分号;的问题
阅读量:673 次
发布时间:2019-03-16

本文共 437 字,大约阅读时间需要 1 分钟。

当我们在编写代码时,某些符号若单独以代码开始,可能会引发语法解析错误。比如,(、[以及键盘上数字1左边的`符号若直接用于代码开头,必须在前面补充分号。这种情况类似于主程序的自动执行代码,这样做可以避免解释器误解代码意图。

一个常见的例子是这样的代码:

(function () {  console.log('hello')})();['开发', '测试'].forEach(function (item) {  console.log(item)})

虽然看起来有些奇怪,但在某些环境下这确实有效。这个技巧在网络外挂、特定库依赖或防止预解析错误等场景中很有用。

关于模板字符串,这是ECMAScript 6引入的一种新字符串格式,支持换行和多变量拼接,极大提升了代码阅读性和灵活性。举例来说:

const test = ` 鹅、鹅、鹅,曲项向天歌,白毛浮绿水,红掌拨清波。` console.log(test)

输出结果与格式完全一致,为开发者提供了更直观的代码组织方式。

转载地址:http://oqeqz.baihongyu.com/

你可能感兴趣的文章
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>
npm node pm2相关问题
查看>>
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>
npm run build报Cannot find module错误的解决方法
查看>>
npm run build部署到云服务器中的Nginx(图文配置)
查看>>
npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
查看>>
npm scripts 使用指南
查看>>
npm should be run outside of the node repl, in your normal shell
查看>>
npm start运行了什么
查看>>
npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
查看>>
npm 下载依赖慢的解决方案(亲测有效)
查看>>