ES6的几个新特征

废话少说,还是直接上代码吧。

1、参数默认值

以前是这么写的:

var link = function (height, color, url) {
    var height = height || 50
    var color = color || 'red'
    var url = url || 'http://azat.co'
    ...
}

很显然以前的写法有点技巧性,现在则可以这么写:

var link = function(height = 50, color = 'red', url = 'http://azat.co') {
  ...
}

很显然跟我们熟悉的C++写法一样了。

2、模板面值

以前跟Java的写法很像:

var name = 'Your name is ' + first + ' ' + last + '.'
var url = 'http://localhost:3000/api/messages/' + id

现在:

var name = `Your name is ${first} ${last}.`
var url = `http://localhost:3000/api/messages/${id}`

注意单引号变成了`,里面的值要用${}包起来。

3、字符串多行时的情况

以前用+解决:

var roadPoem = 'Then took the other, as just as fair,\n\t'
    + 'And having perhaps the better claim\n\t'
    + 'Because it was grassy and wanted wear,\n\t'
    + 'Though as for that the passing there\n\t'
    + 'Had worn them really about the same,\n\t'

var fourAgreements = 'You have the right to be you.\n\
    You can only be you when you do your best.'

现在用逗号解决:

var roadPoem = `Then took the other, as just as fair,
    And having perhaps the better claim
    Because it was grassy and wanted wear,
    Though as for that the passing there
    Had worn them really about the same,`

var fourAgreements = `You have the right to be you.
    You can only be you when you do your best.`

注意外面还是用“`”。

4、分解赋值

以前吧就得分开赋值:

var jsonMiddleware = require('body-parser').json

var body = req.body, // body has username and password
var username = body.username,
var password = body.password 

现在用不着了,它自己可以找到对应的值:

var {house, mouse} = $('body').data() // we'll get house and mouse variables
var {json: jsonMiddleware} = require('body-parser')
var {username, password} = req.body

5、箭头函数

以前的:

var _this = this
$('.btn').click(function(event){
  _this.sendData()
})

现在的:

$('.btn').click((event) =>{
  this.sendData()
})

这里多看一个例子,以前函数返回必须用return关键字,现在如果条件允许的话就可以不用return了:

var ids = ['5632953c4e345e145fdf2df8', '563295464e345e145fdf2df9'];
var messages = ids.map(function (value, index, list) {
  return 'ID of ' + index + ' element is ' + value + ' ' // explicit return
})

现在有了箭头函数以后是这样的:

var ids = ['5632953c4e345e145fdf2df8','563295464e345e145fdf2df9']
var messages = ids.map((value, index, list) => `ID of ${index} element is ${value} `) // implicit return

6、Promise

下面这个函数很熟悉:

setTimeout(function(){
  console.log('Yay!')
}, 1000)

很显然是异步延迟的嘛,那么现在用promise怎么写呢?

var wait1000 =  new Promise(function(resolve, reject) {
  setTimeout(resolve, 1000)
}).then(function() {
  console.log('Yay!')
})

如果再改为箭头函数的话:

var wait1000 =  new Promise((resolve, reject)=> {
  setTimeout(resolve, 1000)
}).then(()=> {
  console.log('Yay!')
})

let与var的作用范围

我们知道var一般写在变量前面,但是这个var所控制变量的生命周期着实让初学者头疼,因为跟我们常见的Java或者C++变量定义的范围不一样嘛,var的生命周期是在函数范围内的,也就是说,var一旦定义了,跟函数的生命周期是一致的,所以为了避免这种不必要的麻烦,回归到Java或者C++语言变量生命周期的可控性,那么我们就用let代替var:

function calculateTotalAmount (vip) {
  var amount = 0 // probably should also be let, but you can mix var and let
  if (vip) {
    let amount = 1 // first amount is still 0
  } 
  { // more crazy blocks!
    let amount = 100 // first amount is still 0
    {
      let amount = 1000 // first amount is still 0
      }
  }  
  return amount
}

console.log(calculateTotalAmount(true))

也就是说在一个代码块定义的,生命周期只能在代码块范围

参考文献

https://webapplog.com/es6/

### 回答1: 在ES6中,可以采用以下几种方法来手写数组去重: 1. 使用Set数据结构: 使用Set数据结构是一种简单且高效的方法,由于Set不允许重复值,因此可以利用这一特性快速实现数组去重: ```javascript const arr = [1, 2, 3, 4, 5, 1, 2, 3]; const uniqueArr = [...new Set(arr)]; ``` 2. 使用filter方法: 利用数组的filter方法结合indexOf可以实现数组去重。 ```javascript const arr = [1, 2, 3, 4, 5, 1, 2, 3]; const uniqueArr = arr.filter((item, index) => arr.indexOf(item) === index); ``` 3. 使用reduce方法: 利用reduce方法遍历数组,将不重复的值保存在一个的数组中。 ```javascript const arr = [1, 2, 3, 4, 5, 1, 2, 3]; const uniqueArr = arr.reduce((acc, cur) => { if (!acc.includes(cur)) { acc.push(cur); } return acc; }, []); ``` 这些方法都可以在ES6中手写实现数组去重,具体选择哪种方法取决于实际的需求和性能要求。 ### 回答2: ES6提供了几种去重数组的方法,下面我将手写几个常用的数组去重方法。 方法一:使用Set对象 ```javascript let arr = [1, 2, 3, 3, 4, 4, 5]; let uniqueArr = Array.from(new Set(arr)); console.log(uniqueArr); // [1, 2, 3, 4, 5] ``` 方法二:使用filter函数和indexOf方法 ```javascript let arr = [1, 2, 3, 3, 4, 4, 5]; let uniqueArr = arr.filter((item, index) => arr.indexOf(item) === index); console.log(uniqueArr); // [1, 2, 3, 4, 5] ``` 方法三:使用reduce函数 ```javascript let arr = [1, 2, 3, 3, 4, 4, 5]; let uniqueArr = arr.reduce((prev, curr) => prev.includes(curr) ? prev : [...prev, curr], []); console.log(uniqueArr); // [1, 2, 3, 4, 5] ``` 方法四:使用利用Map对象 ```javascript let arr = [1, 2, 3, 3, 4, 4, 5]; let map = new Map(); arr.forEach(item => map.set(item, item)); let uniqueArr = [...map.values()]; console.log(uniqueArr); // [1, 2, 3, 4, 5] ``` 以上就是我手写的几个利用ES6实现数组去重的方法。这些方法都能有效地去除数组中的重复元素。 ### 回答3: 在ES6中,有许多方法可以用来手写数组去重。以下是其中几种常见的方法: 1. 使用Set对象:ES6增的Set对象可以用来存储唯一的值。可以将数组转换为Set对象,然后将Set对象再转换回数组,即可实现数组的去重。 ```javascript const arr = [1, 2, 2, 3, 4, 4, 5]; const uniqueArr = Array.from(new Set(arr)); console.log(uniqueArr); // [1, 2, 3, 4, 5] ``` 2. 使用reduce方法:使用reduce方法遍历数组,将每个元素添加到一个空数组中,但之前先检查该元素是否已存在于数组中。 ```javascript const arr = [1, 2, 2, 3, 4, 4, 5]; const uniqueArr = arr.reduce((acc, cur) => { if (!acc.includes(cur)) { acc.push(cur); } return acc; }, []); console.log(uniqueArr); // [1, 2, 3, 4, 5] ``` 3. 使用filter方法:使用filter方法遍历数组,根据元素在数组中的索引位置进行判断,只保留第一次出现的元素。 ```javascript const arr = [1, 2, 2, 3, 4, 4, 5]; const uniqueArr = arr.filter((value, index, self) => { return self.indexOf(value) === index; }); console.log(uniqueArr); // [1, 2, 3, 4, 5] ``` 这些方法都可以有效地实现数组的去重,根据实际情况选择适合的方法使用即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值