您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

javascript 没有类吗

2024/11/7 3:42:31发布22次查看
在ecmascript6规范之前,javascript没有类的概念,仅允许通过构造函数来模拟类,通过原型实现继承。而ecmascript6后,可以使用class关键字来定义类,使用class关键字定义类的写法更加清晰,更像面向对象的语法。
本教程操作环境:windows7系统、ecmascript 6版、dell g3电脑。
javascript 是基于对象,但不完全面向对象的编程语言。在 js 面向对象的编程模式中,有两个核心概念: 对象和类。在 ecmascript6 规范之前,javascript 没有类的概念,仅允许通过构造函数来模拟类,通过原型实现继承。
在es6中新增了class关键字用来定义类,使用class关键字定义类的写法更加清晰,更像面向对象的语法。但是可以看作是语法糖,因为它还是构造函数和原型的概念。
1 类声明定义类有2中方式,类声明和类表达式:
// 类声明class student {}// 类表达式const student = class {}
2 为什么说它是语法糖因为类实际上它是一个function,区别在于构造函数是函数作用域,类是块级作用域,类中的方法,都是定义在类的prototype上面,所以文章开头说它还是构造函数和原型的概念:
class student { take() {}}const a = new student()console.log(typeof student) // functionconsole.log(a.take === student.prototype.take) // true// 同等于function student() {}student.prototype.take = function() {}const a = new student()console.log(typeof student) // functionconsole.log(a.take === student.prototype.take) // true
3 类包含的属性和方法类可以包含构造函数方法、实例方法、获取函数、设置函数和静态类方法,但这些都不是必需的。空的类定义照样有效。
class student { // 实例属性 也可以放在这 // b = 1 // 静态属性 static a = 1 // 构造函数 constructor() { // 实例属性 - 也可以放在类的最顶层 this.b = 1 } // 获取函数 get myname() {} // 设置函数 set myname() {} // 静态方法 不会被实例继承 static show() {} // 方法 take() {} // 私有方法 _take() {}}
3.1 类的构造函数
类的构造函数关键字是constructor,它同等于原型中的prototype.constructor。
如果没有写constructor函数,那么会默认有一个空的constructor函数。
class a { constructor() { this.name = '小明' }}const b = new a()b.constructor === a.prototype.constructor // true
当使用new操作符创建实例时,会调用constructor构造函数。
3.2 类的方法
class student { // 方法 take() {}}
3.3 类的静态方法
跟类的方法一样,只不过前面加上static关键字。
静态方法不会被实例继承。
父类的静态方法可以被子类继承。
class a { // 静态方法 static show() { console.log('hi') }}class b extends a {}const c = new a()c.show() // c.show is not a functionb.show() // hi
3.4 类的私有方法
es6中没有提供这个方法,但是通常都是在方法前面加上下划线来表示。
class a { // 私有方法 _show() { console.log('hi') }}
3.5 取值函数(getter)和存值函数(setter)
在类中有set和get关键词,可以对某个属性设置存值和取值函数,拦截它的存取行为。
class a { constructor () { this.name = '小米' } get name () { return 'get' } set name (val) { console.log('set' + val) }}const b = new a()b.name // getb.name = 123 // set123
4 类的继承4.1 通过extends实现继承
类的继承通过extends关键字。
class a { // 静态方法 static show() { console.log('hi') }}class b extends a {}
4.2 super方法
注意如果子类如果没写constructor构造函数,则会默认有constructor构造函数和super方法,但是如果显性的写了constructor构造函数,那么必须在子类的构造函数中添加super方法,添加之后会调用父类的构造函数并得到父类的属性和方法,如果没有添加super方法则会报referenceerror错误。
class a { constructor () { this.name = '小米' } show() { console.log('hi') }}class b extends a { constructor () { super() // 如果不写super,则会报referenceerror错误 }}const c = new b()
super方法中也可以传参
class a { constructor (name) { this.name = name } show() { console.log('hi') }}class b extends a { constructor () { super('小红') }}const c = new b()c.name // 小红
5 其他5.1 方法中的this指向
类的方法中如果有this,那么它指向的是类的实例。但是如果将它单独拿出来使用那么会报错。
class a { constructor () { this.name = '小米' } show () { console.log(this.name) }}const b = new a()b.show() // 小米const { show } = b // cannot read property 'name' of undefined
解决办法有2种:
1、在构造函数中绑定this
class a { constructor () { this.name = '小米' this.show = this.show.bind(this) } show () { console.log(this.name) }}
2、使用箭头函数
class a { constructor () { this.name = '小米' this.show = () => this } show () { console.log(this.name) }}
5.2 区分是否继承了这个类
区分是否继承了这个类使用object.getprototypeof函数。
class a { constructor () { this.name = '小米' } show() { console.log('hi') }}class b extends a { constructor () { super() }}class c {}object.getprototypeof(b) === a // true 是继承的a类object.getprototypeof(b) === c // false 没有继承c类
【推荐学习:javascript高级教程】
以上就是javascript 没有类吗的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product