class
constructor construction method
this represents the instance object
There is no need to separate the methods with commas, and an error will be reported if they are added.
typeof Point // "function"
Point === Point.prototype.constructor // true
The data type of the class is the function, and the class itself points to the constructor.
All methods of a class are defined on prototype
the properties of the class
All methods defined inside the class are non-enumerable
Methods defined inside ES6 classes are not enumerable; ES5 can
# es5
console.log(Object.getPrototypeOf(a));//['es5method']
#es6
console.log(Object.getPrototypeOf(a));//[]
point.hasOwnProperty('toString') // false
constructor method
class default method
When an object instance is generated by the new command, this method is called by itself.
If not explicitly defined, it will be added by default
The constructor method returns the instance object (ie this) by default
The properties of an instance are defined on the prototype unless are explicitly defined on itself (that is, defined on this
the object).
The class must be new
called, otherwise an error will be reported.
All instances of a class share a prototype object
Value function (getter) and storage function (setter)
class Point{
get prop() {
return 'getter'; }
set prop(value) {
console.log('setter: '+value); }
}
important point
strict mode
Inside classes and modules, strict mode is the default, so there is no need to use a use strict
specified runtime mode.
no boost
Class does not exist variable hoisting (hoist)
The reason for this provision is related to the inheritance mentioned below, which must ensure that the subclass is defined after the parent class.
Class expression
let Poin = class Point {
getName() {
console.log(Point.name, 'name')
}
}
let point = new Poin()
point.getName() // Point
console.log(Poin.name, 'Poin.name');//Point
console.log(Point1.name, 'Poin.name');//Point is not defined
Point
It is only available inside Class and refers to the current class. Outside the Class, this class can only be used by Poin
reference.
what this points to
If the method of the class contains it this
, it points to the instance of the class by default.
However, if this method is extracted and used alone, this
it will point to the environment where the method runs
Inside an arrow function this
always points to the object in which it was defined.
static method
static
Keyword, will not be inherited by the instance, but called directly through the class
If the static method contains this
keywords, this this
refers to the class, not the instance.
A static method can have the same name as a non-static method.
The static method of the parent class can be inherited by the subclass.
Static methods are also super
callable from objects.
New way of writing instance attributes
class Point {
name;
constructor() {
/**/}
}
static property
class Foo {
}
Foo.prop = 1
# 或者
class Foo {
prop = 1;
}
Private Methods and Private Properties
Methods and properties that can only be accessed inside the class, not outside. Conducive to code encapsulation
Before the property name, use #
the denotation
class Foo {
#count = 0
}
Can only be used inside a class (
this.#count
). If used outside the class, an error will be reported
Private properties can also have getter and setter methods.
new.target Property
This attribute is generally used in constructors
Returns new
the constructor that the command acts on. Returns if the constructor was not invoked by command new
or call , so this property can be used to determine how the constructor was invoked.Reflect.construct()
new.target
undefined
function Person(name) {
if (new.target !== undefined) {
this.name = name;
} else {
throw new Error('必须使用 new 命令生成实例');
}
}
var person = new Person('张三'); // 正确
var notAPerson = Person.call(person, '张三'); // 报错
Called internally by Class
new.target
to return the current Class.
When a subclass inherits from a parent class, new.target
the subclass is returned.
class Fu {
constructor() {
console.log( new.target) //Son
}
}
class Son extends Fu {
constructor () {
super()
}
}
Class inheritance
extends
Inheritance through keywords
super
Keyword: Indicates the constructor of the parent class, which is used to create this
an object of the parent class.
The subclass must constructor
call super
the method in the method, otherwise an error will be reported when creating a new instance.
This is because the object of the subclass this
must first be shaped through the constructor of the parent class to obtain the same instance attributes and methods as the parent class, and then processed to add the instance attributes and methods of the subclass itself. If super
the method is not called, the subclass cannot get this
the object.
In the constructor of the subclass, keywords super
can only be used after calling, otherwise an error will be reported. this
This is because the subclass instance is constructed based on the parent class instance, and only super
methods can call the parent class instance.
class Son extends Fu {
constructor() {
this.color = color // ReferenceError
super(x,y)
}
}
The static method of the parent class will also be inherited by the subclass.
Object.getPrototypeOf()
The static method of the parent class will also be inherited by the subclass.
Object.getPrototypeOf(ColorPoint) === Point
// true
super keyword
It can be used as a function or as an object.
when called as a function
When called as a function, it represents the constructor of the parent class, and super
the internal this
one refers to B
the instance of
It can only be used in the constructor of a subclass, and an error will be reported if it is used in other places.
class A {
}
class B extends A {
m() {
super(); // 报错
}
}
as an object
In ordinary methods, it points to the prototype object of the parent class ; in static methods, it points to the parent class
. If a property is assigned through super, then super is this , and the assigned property will become the property of the subclass instance.
class A {
constructor() {
this.p = 2}
say() {
console.log('say', p)}
}
A.prototype.name = "Ren"
class B {
constructor() {
super()
this.p = 99
console.log(super.x) //undefined
super.say() //99
console.log(super.name) //Ren
this.b = 1
super.b = 2
console.log(super.b)//undefined
console.log(this.b)//2
}
}
Failed to get attribute p
Get method say success
Get property name successfully
Note: Although super.say() calls A.prototype.say(), but the internal this of A.prototype() points to B, so the output is 99
Since this
it points to a subclass instance, if you super
assign a value to a property, then super
it isthis
If super
it is used as an object in a static method, it super
will point to the parent class, not the prototype object of the parent class.
class Point {
static age = 18;
name = 19
static say() {
console.log(this.age, 'age')//18
console.log(this.name, 'name')//undefined
}
}
class Son extends Point {
static say() {
super.say()
}
}
Son.say()
When the method of the parent class is called in the static method super
of the subclass, the internal method this
points to the current subclass, not the instance of the subclass.
Prototype and __proto__ properties of classes
Class, as the syntactic sugar of the constructor, has both prototype
attributes and __proto__
attributes, so there are two inheritance chains at the same time.
(1) The attribute of the subclass __proto__
indicates the inheritance of the constructor and always points to the parent class.
prototype
(2) The attribute of the subclass attribute __proto__
, which means the inheritance of the method, always points to the attribute of the parent class prototype
.
B
These two inheritance chains can be understood as follows: as an object, the prototype ( __proto__
property) of the subclass ( ) is the parent class ( ); as a constructor, the prototype object (property) of A
the subclass ( ) is the prototype object of the parent class ( property) instance.B
prototype
prototype
class A {
}
class B {
}
Object.setPrototypeOf(B.prototype, A.prototype)// B 继承 A 的静态属性
Object.setPrototypeOf(B, A)// B 继承 A 的静态属性
const b = new B()
Object.setPrototypeOf
implementation of the method.
Object.setPrototypeOf = function (obj, proto) {
obj.__proto__ proto
return obj
}
Inheritance of native constructors
I did not see
Implementation of the Mixin pattern
I did not see