程序中类
ES6
面向对象 ,类
属性
方法
函数模拟
人: Person
属性: name
展示名字: showName
Person.prototype.showName
ES5之前:
function Person(){
this.name='aaa';
}
Person.prototype.showName=function(){}
ES6中变形:
class Person{
constructor(){
this.name = 'aaa';
}
showName(){
}
}
--------------------------
const Person = class{}
------------------------------
let a = 'strive';
let b = 'method';
class Person{
[a+b](){
}
}
let aaa='aaa';
let bbb='ddd';
let json={
[aaa+bbb]:'welcome 51mmr.net'
}
注意:
1. ES6里面class没有提升功能,在ES5,用函数模拟可以,默认函数提升
2. ES6里面this 比之前轻松多了
矫正this:
1. fn.call(this指向谁, args1, args2....)
2. fn.apply(this指向谁, [args1, args2....])
3. fn.bind()
------------------------------------------------------------
class里面取值函数(getter), 存值函数(setter)
------------------------------------------------------------
静态方法: 就是类身上方法
static aaa(){
}
父类.aaa();
------------------------------------------------------------
父类
子类
------------------------------------------------------------
继承:
之前:
Person
Student
现在: extends
class Student extends Person{
}
ES6总结--第三部分之类
最新推荐文章于 2025-06-29 16:21:57 发布
本文深入探讨ES6中的面向对象编程,详细介绍类的定义、属性、方法及构造函数。对比ES5与ES6中类的实现方式,解析getter、setter、静态方法及继承的概念与应用。
379

被折叠的 条评论
为什么被折叠?



