一些JS简写方式

1.数据类型转换

~是按位取反的意思,计算机里面处理二进制数据时候的非,~~就是再转回来,利用两个按位取反的符号,进行类型的转换,转换成number类型,并向下取整。

~~3.14    ==>  3    //此处可以替代Math.round()方法
~~'1234'  ==>  1234 //此处可以替代parseInt()方法
~~''      ==>  0
~~false   ==>  0

隐式转换数据类型

+'123456'    ==>  123456   //String转换为Nunber
123456+''    ==>  '123456' //Number转化为String

2.十进制指数

当需要写数字带有很多零时(如10000000),可以采用指数(1e7)来代替这个数字

3.对象属性简写

如果属性名与key名相同,则可以采用ES6的方法:

const obj = { x:x, y:y };

简写:

const obj = { x, y };

4.箭头函数

一般写法:

setTimeout(function() {
 console.log('Loaded')
}, 2000);
 
list.forEach(function(item) {
 console.log(item);
});

简写:

setTimeout(() => console.log('Loaded'), 2000);
list.forEach(item => console.log(item));

5.默认参数值

为了给函数中参数传递默认值,通常使用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

6.模板字符串

传统的JavaScript语言,输出模板通常是这样写的。

const db = 'http://' + host + ':' + port + '/' + database;

简写:

const db = `http://${host}:${port}/${database}`;

 7.解构赋值简写方法

在web框架中,经常需要从组件和API之间来回传递数组或对象字面形式的数据,然后需要解构它

const observable = require('mobx/observable');
const action = require('mobx/action');
const runInAction = require('mobx/runInAction');
 
const store = this.props.store;
const form = this.props.form;
const loading = this.props.loading;
const errors = this.props.errors;
const entity = this.props.entity;

简写:

import { observable, action, runInAction } from 'mobx';
const { store, form, loading, errors, entity } = this.props;

也可以使用解构赋值来便捷书写变量赋值与交换

let a=1,b=2;
[a,b] = [3,4]   //赋值结果 a=3 , b=4
[a,b] = [b,a]  //交换结果  a=4 , b=3

8.多行字符串简写

const lorem = 'Lorem ipsum dolor sit amet, consectetur\n\t'
 + 'adipisicing elit, sed do eiusmod tempor incididunt\n\t'
 + 'ut labore et dolore magna aliqua. Ut enim ad minim\n\t'
 + 'veniam, quis nostrud exercitation ullamco laboris\n\t'
 + 'nisi ut aliquip ex ea commodo consequat. Duis aute\n\t'
 + 'irure dolor in reprehenderit in voluptate velit esse.\n\t'

简写:

const lorem = `Lorem ipsum dolor sit amet, consectetur
 adipisicing elit, sed do eiusmod tempor incididunt
 ut labore et dolore magna aliqua. Ut enim ad minim
 veniam, quis nostrud exercitation ullamco laboris
 nisi ut aliquip ex ea commodo consequat. Duis aute
 irure dolor in reprehenderit in voluptate velit esse.`

9.Array.find简写

想从数组中查找某个值,则需要循环。在ES6中,find()函数能实现同样效果。

const pets = [
 { type: 'Dog', name: 'Max'},
 { type: 'Cat', name: 'Karl'},
 { type: 'Dog', name: 'Tommy'},
]
 
function findDog(name) {
 for(let i = 0; i<pets.length; ++i) {
 if(pets[i].type === 'Dog' && pets[i].name === name) {
 return pets[i];
 }
 }
}

简写:

pet = pets.find(pet => pet.type ==='Dog' && pet.name === 'Tommy');
//只用pet.type ==='Dog'判断也可以
console.log(pet); // { type: 'Dog', name: 'Tommy' }

10.扩展运算符简写

扩展运算符有几种用例让JavaScript代码更加有效使用,可以用来代替某个数组函数。

// joining arrays
const odd = [1, 3, 5 ];
const nums = [2 ,4 , 6, ...odd];
console.log(nums); // [ 2, 4, 6, 1, 3, 5 ]
 
// cloning arrays
const arr = [1, 2, 3, 4];
const arr2 = [...arr];

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值