简写:
- sayHello = name => console.log('Hello', name);
- setTimeout(() => console.log('Loaded'), 2000);
- list.forEach(item => console.log(item));
10.隐式返回值简写
经常应用return语句来返回函数最终结不雅,一个零丁语句的箭头函数能隐式返回其值5悲数必隆盛略{}为了省略return关键字)
- function calcCircumference(diameter) {
- return Math.PI * diameter
- }
- var func = function func() {
- return { foo: 1 };
- };
简写:
11.默认参数值
为了给函数中参数传递默认值,平日应用if语句来编写,然则应用ES6定义默认值,则会很简洁:
- function volume(l, w, h) {
- if (w === undefined)
- w = 3;
- if (h === undefined)
- h = 4;
- return l * w * h;
- }
- volume = (l, w = 3, h = 4 ) => (l * w * h);
- volume(2) //output: 24
12.模板字符串
传统的JavaScript说话,输出模板平日是如许写的。
- const welcome = 'You have logged in as ' + first + ' ' + last + '.'
- const db = 'http://' + host + ':' + port + '/' + database;
ES6可以应用反引号和${}简写:
- calcCircumference = diameter => (
- Math.PI * diameter;
- )
- var func = () => ({ foo: 1 });
- const welcome = `You have logged in
推荐阅读
【限时免费】岁尾最强一次云计算大年夜会,看传统、社区、互联网企业若何碰撞? 收集安然市场正在飞速成长,这一点已成为事实。捷报道,2017 年中国的互联网平顺家当收入达到457.1亿元人平>>>详细阅读
本文标题:19个JavaScript有用的简写技术
地址:http://www.17bianji.com/lsqh/40248.html
1/2 1

网友点评
精彩导读
科技快报
品牌展示