JS 总结之 class
栏目: JavaScript · 发布时间: 7年前
内容简介:class 是 ES6 的新特性,可以用来定义一个类,实际上,class 只是一种语法糖,它是构造函数的另一种写法。用法和使用构造函数一样,通过 new 来生成对象实例每个类都必须要有一个 constructor,如果没有显示声明,js 引擎会自动给它添加一个空的构造函数:
class 是 ES6 的新特性,可以用来定义一个类,实际上,class 只是一种语法糖,它是构造函数的另一种写法。
class Person {
}
typeof Person // "function"
Person.prototype.constructor === Person // true
复制代码
:car: 使用
用法和使用构造函数一样,通过 new 来生成对象实例
class Person {
}
let jon = new Person()
复制代码
:bus: constructor
每个类都必须要有一个 constructor,如果没有显示声明,js 引擎会自动给它添加一个空的构造函数:
class Person {
}
// 等同于
class Person {
constructor () {
}
}
复制代码
属性和方法
定义于 constructor 内的属性和方法,即定义在 this 上,属于实例属性和方法,否则属于原型属性和方法。
class Person {
constructor (name) {
this.name = name
}
say () {
console.log('hello')
}
}
let jon = new Person()
jon.hasOwnPrototype('name') // true
jon.hasOwnPrototype('say') // false
复制代码
:police_car: 属性表达式
let methodName = 'say'
class Person {
constructor (name) {
this.name = name
}
[methodName] () {
console.log('hello')
}
}
复制代码
:truck: 静态方法
不需要通过实例对象,可以直接通过类来调用的方法,其中的 this 指向类本身
class Person {
static doSay () {
this.say()
}
static say () {
console.log('hello')
}
}
Person.doSay() // hello
复制代码
静态方法可以被子类继承
// ...
class Sub extends Person {
}
Sub.doSay() // hello
复制代码
可以通过 super 对象访问
// ...
class Sub extends Person {
static nice () {
return super.doSay()
}
}
Sub.nice() // hello
复制代码
:tractor: 严格模式
不需要使用 use strict,因为只要代码写在类和模块内,就只能使用严格模式。
提升
class 不存在变量提升。
new Person() // Uncaught ReferenceError: Person is not defined
class Person {
}
复制代码
:bullettrain_side: name 属性
name 属性返回了类的名字,即紧跟在 class 后面的名字。
class Person {
}
Person.name // Person
复制代码
:light_rail: this
默认指向类的实例。
:steam_locomotive: 取值函数(getter)和存值函数(setter)
class Person {
get name () {
return 'getter'
}
set name(val) {
console.log('setter' + val)
}
}
let jon = new Person()
jon.name = 'jon' // setter jon
jon.name // getter
复制代码
class 表达式
如果需要,可为类定义一个类内部名字,如果不需要,可以省略:
// 需要在类内部使用类名
const Person = class Obj {
getClassName () {
return Obj.name
}
}
// 不需要
const Person = class {}
复制代码
立即执行的 Class:
let jon = new class {
constructor(name) {
this.name = name
}
sayName() {
console.log(this.name)
}
}('jon')
jon.sayName() //jon
复制代码
以上所述就是小编给大家介绍的《JS 总结之 class》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。