1. ES6 變量聲明:var let,let 是為了取代var ,但是兼容性不好
var 只有一層作用域螺戳,let有多層作用域
{}是為了聲明作用域
{
let a = 10;
var b =15;
console.log(a,b);
}
console.log(b);
console.log(a);//找不到
2. 常量:不能被重復(fù)定義
const PI = "3.14159265358979323846";
3. 字符串模板
let name = "張三";
let str2 = `姓名是${name}年齡是19`;
4. 對象簡寫
var name = "張三";
var obj1 = {
name糊治;
eat(){
console.log("吃");
}
}
5 . 工廠模式:防止變量污染
function Factory(height){
//obj人類;
var obj = {};
obj.height = height;
obj.name = "張三";
obj.age = 20;
obj.hobby = function(){
console.log("喜歡");
return obj;
}
var newFactory = Factory("178cm");
6. 箭頭函數(shù)
// ES5寫法(有參函數(shù))
var test = function(str){
return str;
}
// ES6寫法 必須是匿名函數(shù)
var test = (str) =>str;
console.log(test("hello"));
// ES5寫法(無參函數(shù))
var test1 = function(){
var str = "你好";
return str;
}
// ES6寫法
var test1 = () =>{
var str = "你好";
return str;
}
// 作用域和對象沖突:用小括號包裹返回對象,否則當(dāng)成作用域解析,報(bào)錯(cuò)
var test = () =>({
name:"張三",
age:17,
});
7. 箭頭函數(shù)的this穿透
var obj = {
name:"張三",
age:19,
// es5 this指向該對象
action:function(){
console.log(this);
}
// es6 箭頭函數(shù) this指向windod this向上一層穿透
action:() => {
console.log(this);
}
}
8. 隱藏參數(shù)
// ES5 隱藏參數(shù)
function test(){
console.log(arguments);
}
test("1111","2222");
// ES6 展開運(yùn)算符
function test1(...arr){
console.log(arr);
}
test1("3333333","44444");
9. 類
class Person{
//類最開始在加載的時(shí)候執(zhí)行
constructor(name,age){
this.name = name;
this.age = age;
}
hobby(){
console.log("喜歡");
}
showName(){
console.log(this.name);
}
}
var zs = new Person("zs",28);
console.log(zs.age);
zs.showName();
//類的繼承
class Student extends Person{
constructor(name,age){
//super傳參缺前,繼承屬性
super(name,age);
}
action(){
console.log("我是action函數(shù)");
}
}
var newStudent = new Student("李四",222);
console.log(newStudent.name);
newStudent.hobby();