(1).let、const用法
<script>
var a = 2;
{
let a = 3;
alert(a);
}
alert(a);
//let 允许创建块级作用域,ES6 推荐在函数中使用 let 定义变量,let定义的变量不在变量提升
const NUM = 1;
NUM = 4;
//const 声明的常量类似于指针,它指向某个引用,也就是说这个「常量」并非一成不变的
</script>
有几个点需要注意:
let 关键词声明的变量不具备变量提升(hoisting)特性
let 和 const 声明只在最靠近的一个块中(花括号内)有效
当使用常量 const 声明时,请使用大写变量,如:CAPITAL_CASING
const 在声明时必须被赋值
(2)箭头函数
<script>
var fun = (num) =>num
console.log(fun(5));
var add = (num1,num2) => num1+num2
console.log(fun(3,4))
</script>
箭头函数:
不可以当作构造函数,也就是说,不可以使用new命令,否则会抛出一个错误。
不可以使用arguments对象,该对象在函数体内不存在。如果要用,可以用Rest参数代替。
不可以使用yield命令,因此箭头函数不能用作Generator函数。
(3)class类
class Person {
constructor (name, age) {
this.name = name;
this.age = age;
}
say () {
console.log('My name is ' + this.name + ', I`m ' + this.age + ' years old');
}
}
let student = new Person("laowang","20")
console.log(student.say());
继承extends
<script>
class Person {
constructor(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
incrementAge() {
this.age += 1;
}
}
var studentName = new Person("laowang","22","male");
console.log(studentName)
class Personal extends Person {
constructor(name, age, gender, occupation, hobby) {
super(name, age, gender);
this.occupation = occupation;
this.hobby = hobby;
}
incrementAge() {
super.incrementAge();
this.age += 20;
console.log(this.age);
}
}
var studentName2 = new Personal("laoli","20","female","student","football")
console.log(studentName2);
</script>
1.extends 允许一个子类继承父类,需要注意的是,子类的 constructor 函数中需要执行 super() 函数。
当然,子类方法中也可以调用父类的方法
ES6新增属性笔记一
最新推荐文章于 2023-08-16 20:39:03 发布