Tell interesting knowledge in your own words.
Hi everyone, I am 梅巴哥er
. This article uses the form of storytelling to talk about class-related knowledge to deepen memory. Hope that helps.
First introduce the related concepts:
-
What is a class?
- The class class is a new concept in ES6
- A class abstracts the public part of an object, and it refers to an example in general.
- For example, celebrities are a category of people (a big category), and Di Lieba, who is a celebrity, is the object. The object is a concrete concept.
- The common attributes and methods in the class must be used with this
-
Class creation
class Star {
...
}
// 用class创建
// 类名首字母大写
// 类名后不跟()
- Class inheritance
class Father{
...
}
class Son extends Father {
...
}
// 子类继承了父类的属性和方法
// 子类也是类
- Instantiation of the class
class Star {
...
}
var dili = new Star()
// 用new对类实例化
// 实例化的对象可以调用类的属性和方法
- Class constructor
class Star {
constructor(name, age) {
this.name = name
this.age = age
}
}
var dili = new Star('迪丽热巴', 18)
console.log(dili.name + dili.age + '岁了!')
// 输出 迪丽热巴18岁了!
// 每个类都有一个constructor()构造函数
// 创建类的时候会自动生成constructor,写不写都会有
// 类的参数放在constructor里,类的实例可以直接拿去用
// constructor里面的this指向实例对象,方法里面的this指向这个方法的调用者。
- Class super function
class Father {
constructor(x, y) {
this.x = x
this.y = y
}
sum() {
return this.x + this.y
}
}
class Son extends Father {
sum() {
// return 4 // 输出4
return super.sum() // 输出3
}
}
var son = new Son(1, 2)
console.log(son.sum())
// super作用是调用父类
// super.sum()相当于Father.sum()
// 调用的规则是:
// 如果子类中有自己的方法,则输出子类自身方法的结果
// 如果子类没有自己的方法,则输出父类方法的结果
Isn't it silly to see? It doesn't matter, let's look at a short story.
The director planned to make a movie and started looking for stars.
class Star{
}
Stars who want to audition need to fill in their own information, such as name, age, singing or acting experience.
class Star {
constructor(name ,age, film) {
this.name = name
this.age = age
this.film = film
}
sing(song) {
return '会唱的歌曲:' + song
}
show() {
return '参演的影视剧:' + this.film
}
}
// 注意看sing和show方法的参数
// 俩方法的参数放置不一样,用法也不一样
// 因为this指向是不同的
// 参数放在constructor里,this指向的就是实例对象
// 也就是当用new来实例化的时候,这个参数要带上
// 而参数放在方法里,this是指向调用者的,
// 调用的时候再加上实参即可
One day, Di Lieba saw this advertisement for looking for a star to make a drama, so he filled in the information and submitted an application.
// 对类进行实例化
var dili = new Star('迪丽热巴', 18, '好孩子')
console.log(dili.sing('难忘今宵'))
console.log(dili.show())
// 信息:我叫迪丽热巴,18岁,演过好孩子,会唱难忘今宵
// 注意看,sing方法是调用的时候输入参数的,不在实例对象里
// show方法是在实例化的时候加的参数
console.log(dili)
// 输出:Star { name: '迪丽热巴', age: 18, film: '好孩子' }
// 这里面没有唱歌的参数。
The director was very satisfied with Di Lieba, so he asked him for an interview. Then ask him, what else would you do?
class Star {
constructor(name ,age, film) {
this.name = name
this.age = age
this.film = film
}
sing(song) {
return '会唱的歌曲:' + song
}
show() {
return '参演的影视剧:' + this.film
}
}
// 继承Star类
class Dili extends Star {
// 需要在构造器里写上父类的参数
// 如果子类也需要用到参数,也写进构造器里
constructor(name ,age, film,sl, da) {
// super用来调用父类的构造函数时
// 必须写在this之前
// 如果父类里有参数,super里也要带上父类的参数
super(name ,age, film)
this.sl = sl
this.da = da
}
sleep() {
return this.sl
}
dance() {
return this.da
}
}
var dili = new Dili('迪丽热巴', 18, '好孩子', '睡懒觉', '跳舞啊')
console.log(dili)
// 输出:Dili { name: '迪丽热巴', age: 18,
// film: '好孩子', sl: '睡懒觉', da: '跳舞啊' }
Dili said that I can still sleep in and dance.
The director felt very satisfied, so he arranged for him to audition for the shot of sleeping late.
the above.