ES6新增属性笔记一

(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() 函数。

当然,子类方法中也可以调用父类的方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值