es6和es5的區別

2021-08-17 07:41:11 字數 2722 閱讀 1639

1、es6新增了let命令,用來宣告變數。它的用法類似於var,但是所宣告的變數,只在let命令所在的**塊內有效。

a;b;/

let不像var存在變數提公升,即變數一定要宣告之後才能使用

2、塊級作用域

es5只有全域性作用域和函式作用域,沒有塊級作用域

3、變數的結構賦值

var [a, b, c] = [1, 2, 3];

4、模板字串

1

2

3

4

5

6

$('#list').html(`

`);

模板字串中還能呼叫函式

1

2

3

4

5

6

functionfn()

`foo $ bar`

// foo hello world bar

5、函式引數的預設值

es51

2

3

4

5

6

7

8

es61

2

3

4

5

6

7

functionlog(x, y ='world')

log('hello')// hello world

log('hello','china')// hello china

log('hello','')// hello world

console.log(x, y);

}

log('hello')// hello world

log('hello','china')// hello china

log('hello','')// hello

在es5裡,如果使用commonjs標準,引入react包基本通過require進行,**類似這樣:

//es5

var react = require("react");

var = react; //引用react抽象元件

var reactnative = require("react-native");

var = reactnative; //引用具體的react native元件

在es6裡,import寫法更為標準

//es6

import react, from

'react';

import from

'react-native'

在es5裡,要匯出乙個類給別的模組用,一般通過module.exports來匯出

//es5

var mycomponent = react.createclass();

module.exports = mycomponent;

在es6裡,通常用export default來實現相同的功能:

//es6

export

default

class

mycomponent

extends

component

引用的時候也類似:

//es5

var mycomponent = require('./mycomponent');

//es6

import mycomponent from

'./mycomponent';

好文要頂

關注我收藏該文

ES6和ES5的區別

es6和es5的區別?1 定義變數 es5中用 var function es6中用 let const class import 沒有變數提公升 同乙個作用域中不可重複宣告 不會給window增加全域性屬性 會形成塊級作用域 const設定的變數值是不可修改的 理解為常量 暫時性死區 2 解構賦值...

es5和es6的區別

最近在看阮一峰的 ecmascript6入門 講真,這本書對初學者真的很有幫助,在這裡也推薦給大家。接下來,我要說的一些例子也大多 於這本書,如果對這本書感興趣的小夥伴,也可以戳這裡哦!1.新增let命令 在es6之前,我們宣告變數都是通過var,es6新增的let命令與var類似,與之不同是,le...

es5與es6的區別

es5和es6對於前端開發來說是經常都會使用的方法,他們為我們開發提供了很多便利的方法和寫法,使我們的 更加的優雅,作為乙個新人就讓我簡單總結一下es5與es6的不同。1.變數 首先在變數方面es6為我們提供了let和const這兩種新的宣告方法,let和const的出現大大的避免了變數汙染的問題,...