什麼是ECMAScript?javascript
ECMAScript是瀏覽器腳本語言的規範,而咱們熟知的js語言,如JavaScript則是規範的具體實現。es6就比如Java的jdk。css
{ var a = 1; let b = 2; } console.log(a) // 1 console.log(b) // Uncaught ReferenceError: b is not defined
var m = 1; var m = 2; let n = 1; let n = 2; console.log(m) //2 console.log(n) //Uncaught SyntaxError: Identifier 'n' has already been declared
console.log(x); var x = 10; // undefined console.log(y); let y = 12; // Uncaught ReferenceError: Cannot access 'y' before initialization
注意:之後要習慣使用let聲明變量vue
const a = 1; console.log(a) //1 a = 2; console.log(a) // Uncaught TypeError: Assignment to constant variable.
const a; a = 1; console.log(a) //Uncaught SyntaxError: Missing initializer in const declaration
let arr = [1, 2, 3]; let [a, b, c] = arr; console.log(a, b, c) //1,2,3
const person = { name: "qiyue", age: 23, language: ['java', 'js', 'css'] } const { name, age, language } = person console.log(name, age, language) //qiyue 23 (3) ["java", "js", "css"]
const person = { name: "qiyue", age: 23, language: ['java', 'js', 'css'] } //從person裏解析出name的值在賦值給abc const { name:abc, age, language } = person console.log(abc, age, language) //qiyue 23 (3) ["java", "js", "css"]
let str = "hello.vue"; console.log(str.startsWith("hello")) //true console.log(str.endsWith("vue")) //true console.log(str.includes("e")) //true console.log(str.includes("hello")) //true
let str = `<span>hello world</span>` console.log(str) // <span>hello world</span>
let info = `我是${abc},今年${age}` console.log(info) //我是qiyue,今年23
let info = `我是${abc},今年${age}` console.log(info) //我是qiyue,今年23
let info = `我是${abc},今年${age}` console.log(info) //我是qiyue,今年23
function fun() { return "這是一個函數"; } let info = `我是${abc},今年${age + 10},我想說:${fun()}` console.log(info) //我是qiyue,今年33,我想說:這是一個函數
function add(a, b = 1) { return a + b; } console.log(add(10)) //11
function fun(...params) { console.log(params.length) } fun(1, 2) // 2 fun(1, 2, 3, 4) //4
//之前 var sum = function (a, b) { c = a + b return c } console.log(sum(2, 3)) // 5 //箭頭函數 var sum2 = (a, b) => a + b; console.log(sum2(2, 4)) // 6
//之前 function hello(person) { console.log("hello" + person.name) } hello(person); //helloqiyue //箭頭函數 let hello2 = params => console.log("hello" + person.name) hello2(person) //helloqiyue //箭頭函數加解構表達式 var hello3 = ({ name }) => console.log("hello" + name) hello3(person) //helloqiyue
const person = { name: "qiyue", age: 23, language: ["java", "js", "css"] } console.log(Object.keys(person)) //["name","age","language"] console.log(Object.values(person)) // ["qiyue",23,Array(3)] console.log(Object.entries(person)) //[Array(2),Array(2),Array(2)]
onst target = { a: 1 } const source1 = { b: 2 } const source2 = { c: 3 } Object.assign(target, source1, source2); console.log(target) //{a: 1, b: 2, c: 3}
//之前 const name = 'sanyue' const age = 21 //將屬性值name,age分別賦給person1對象的name,age,後面是屬性值 const person1 = { name: name, age: age } console.log(person1) //{name: "sanyue", age: 21} //es6:屬性名和屬性值變量名同樣,能夠省略 const person2 = {name,age} console.log(person2) //{name: "sanyue", age: 21}
let person3 = { name: "qiyue", //之前 eat: function (food) { console.log(this.name + "在吃" + food); }, //箭頭函數中this不能使用,用對象.屬性 eat2: food => console.log(person3.name + "在吃" + food), eat3(food) { console.log(this.name + "在吃" + food) } } person3.eat("蘋果") //qiyue在吃蘋果 person3.eat2("香蕉") // qiyue在吃香蕉 person3.eat3("西瓜") //qiyue在吃西瓜
擴展運算符(...)用於取出參數對象全部可遍歷屬性而後拷貝到當前對象java
//拷貝對象(深拷貝) let p1 = { name: "qiyue", age: 23 } let obj = { ...p1 } console.log(obj)//{name: "qiyue", age: 23} //合併對象 let age1 = { age: 24 } let name1 = { name: "qiyue" } let p2 = {} p2 = { ...age1, ...name1 } console.log(p2) //{age: 24, name: "qiyue"} //若是p2中本來有name,age屬性會被覆蓋
let arr = ["1", "3", "4", "23"] arr = arr.map(item => item * 2) console.log(arr) //[2, 6, 8, 46]
語法:arr.reduce(callbace,[initialValue])es6
callback(執行數組中每一個值的函數,包含四個參數)數組
initialValue(做爲第一次調用callback的第一個參數)瀏覽器
let arr1 = [2, 40, -10, 6] let result = arr1.reduce((a, b) => { return a + b }, 10) console.log(result)//48
什麼是模塊化:模塊化就是把代碼進行拆分,方便重複利用。相似Java中的導包,要使用一個包,必須先導包。二Js中沒有包的概念,換來的就是模塊模塊化
模塊的功能主要有兩個命令構成:export和import函數