【笔记】再学JavaScript ES(6-10)全版本语法——Class
2023-09-27 14:26:51 时间
一、类的声明
1.ES5的做法
/*
* ES5类的声明
*/
// 每个子类特有的属性或方法写到父类的内部
let Animal = function (type) {
this.type = type
}
// 共有方法挂到父类的原型链上
Animal.prototype.eat = function () {
console.log('I am eating food')
}
// 实例对象声明
let dog = new Animal('dog')
let monkey = new Animal('monkey')
console.log(dog)
console.log(monkey)
// 子类覆盖父类方法
monkey.constructor.prototype.eat = function () {
console.log('monkey eat')
}
// 子类执行方法时先查找自己的,然后顺着原型链往上找
dog.eat()
monkey.eat()
console.log(typeof Animal) // function
2.ES6的做法
/*
* ES6类的声明
*/
class Animal {
constructor (type) {
this.type = type
}
eat () {
console.log('I am eating food')
}
}
// 实例对象声明
let dog = new Animal('dog')
let monkey = new Animal('monkey')
console.log(dog)
console.log(monkey)
dog.eat()
monkey.eat()
console.log(typeof Animal) // function
二、属性读写
let _age = 4
class Animal {
constructor (type) {
this.type = type
}
get age () {
return _age
}
set age (val) {
if (val < 7 && val > 4) {
_age = val
}
}
}
let dog = new Animal('dog')
console.log(dog)
console.log('修改之前:', dog.age)
dog.age = 5
console.log('修改之后:', dog.age)
三、类的静态方法
1.ES5中类的静态方法
/*
* ES5类的静态方法
*/
let Animal = function (type) {
this.type = type
}
// 共有方法挂到父类的原型链上
Animal.prototype.eat = function () {
// 静态方法只能通过类来使用
Animal.walk()
console.log('I am eating food')
}
// 静态方法通过类来声明
Animal.walk = function () {
console.log('I am walking')
}
// 实例对象声明
let dog = new Animal('dog')
console.log(dog)
dog.eat()
// 类的静态方法无法通过类的实例化对象使用
dog.walk() // dog.walk is not a function
2.ES6中类的静态方法
/*
* ES6类的静态方法
*/
class Animal {
constructor (type) {
this.type = type
}
eat () {
Animal.walk()
console.log('I am eating food')
}
static walk () {
console.log('I am walking')
}
}
// 实例对象声明
let dog = new Animal('dog')
console.log(dog)
dog.eat()
// 类的静态方法无法通过类的实例化对象使用
dog.walk() // dog.walk is not a function
四、类的继承
1.ES5中类的继承
/*
* ES5类的继承
*/
let Animal = function (type) {
this.type = type
}
// 共有方法挂到父类的原型链上
Animal.prototype.eat = function () {
// 静态方法只能通过类来使用
Animal.walk()
console.log('I am eating food')
}
// 静态方法通过类来声明
Animal.walk = function () {
console.log('I am walking')
}
// 子类声明
let Dog = function () {
// 初始化父类的构造函数(call是用来改变this的指向),实现一部分继承
Animal.call(this, 'dog')
this.run = function () {
console.log('I am running')
}
}
// 将父类原型链挂载到子类,实现另一部分继承(引用复制)
Dog.prototype = Animal.prototype
// 子类实例化
let dog = new Dog()
console.log(dog)
dog.eat()
dog.run()
2.ES6中类的继承
/*
* ES6类的继承
*/
class Animal {
constructor (type) {
this.type = type
}
eat () {
Animal.walk()
console.log('I am eating food')
}
static walk () {
console.log('I am walking')
}
}
class Dog extends Animal {
// 若子类相对父类构造方法无新属性或方法,可省略即隐式调用
constructor (type) {
// super代表父类构造函数
super(type)
this.age = 2
}
run () {
console.log('I am running')
}
}
// 实例对象声明
let dog = new Dog('dog')
console.log(dog)
dog.eat()
dog.run()
拓展:
相关文章
- how many types in javascript
- JavaScript - 去掉(删除)URL 路径上指定的参数(传递一个 URL 和要删除的参数名,返回结果)
- JavaScript时间处理
- Javascript 笔记与总结(2-7)对象
- 《单页Web应用:JavaScript从前端到后端》——1.4 小结
- 《网站建设与网页设计从入门到精通Dreamweaver+Flash+Photoshop+HTML+CSS+JavaScript》——3.5 设置头信息
- JavaScript权威指南学习笔记5
- JavaScript每天一练:简易计算机的实现(两种计算机样式)
- 发发关于JavaScript的感慨,随手记几个js知识碎片
- JavaScript复杂判断的更优雅写法
- JavaScript中 == 和 === 的区别
- javascript闭包具体解释
- Javascript的this用法
- 深入理解JavaScript系列(12):变量对象(Variable Object)