1. Class A class is a template for creating objects. The method of generating object instances in function Point(x, y) { this.x = x; this.y = y; } Point.prototype.toString = function () { return '(' + this.x + ', ' + this.y + ')'; }; var p = new Point(1, 1);
As follows: class Point { constructor(x, y) { this.x = x; this.y = y; } toString() { return '(' + this.x + ', ' + this.y + ')'; } } The data type of a class is a function, which itself is a constructor pointing to a function: // ES5 function declaration function Point() { //... } // ES6 class declaration class Point { //.... constructor() { } } typeof Point // "function" Point === Point.prototype.constructor // true The methods defined in the class are attached to class Point { constructor(x, y) { this.x = x; this.y = y; } toString() { return '(' + this.x + ', ' + this.y + ')'; } } Point.prototype = { //.... toString() } var p = new Point(1, 1); p.toString() // (1,1) Another way to define a class is through a class expression // Unnamed/anonymous class let Point = class { constructor(x, y) { this.x = x; this.y = y; } }; Point.name // Point There is an important difference between function declarations and class declarations. Function declarations are hoisted, but class declarations are not hoisted. > let p = new Point(); // No error will be reported if promoted> function Point() {} > > let p = new Point(); // Error, ReferenceError > class Point {} > 1.1 constructor() A class must have class Point { } // Automatically add class Point { constructor() {} } 1.2 Getters and setters As in class User { constructor(name) { this.name = name; } get name() { return this.name; } set name(value) { this.name = value; } } 1.3 this class User { constructor(name) { this.name = name; } printName(){ console.log('Name is ' + this.name) } } const user = new User('jack') user.printName() // Name is jack const { printName } = user; printName() // Error Cannot read properties of undefined (reading 'name') If you want to call it separately without error, one way is to call class User { constructor(name) { this.name = name; this.printName = this.printName.bind(this); } printName(){ console.log('Name is ' + this.name) } } const user = new User('jack') const { printName } = user; printName() // Name is jack
In addition, you can use arrow functions, because this inside an arrow function always refers to the object where it is defined. class User { constructor(name) { this.name = name; } printName = () => { console.log('Name is ' + this.name) } } const user = new User('jack') const { printName } = user; printName() // Name is jack 1.4 Static properties Static properties refer to the properties of the class itself, rather than the properties defined on the instance object class User { } User.prop = 1; User.prop // 1 1.5 Static MethodsYou can define static methods in a class. The method will not be inherited by object instances, but will be called directly through the class. class Utils { static printInfo() { this.info(); } static info() { console.log('hello'); } } Utils.printInfo() // hello Regarding the calling scope restrictions of methods, such as private and public, 2. Inheritance In When inheriting, the subclass must call the class Point3D extends Point { constructor(x, y, z) { super(x, y); // Call the parent class's constructor(x, y) this.z = z; } toString() { return super.toString() + ' ' + this.z ; // Call toString() of the parent class } } The static methods of the parent class will also be inherited by the child class class Parent { static info() { console.log('hello world'); } } class Child extends Parent { } Child.info() // hello world 2.1 super keyword The class Parent {} class Child extends Parent { constructor() { super(); } } When calling the parent class method through class Parent { constructor() { this.x = 1; this.y = 10 } printParent() { console.log(this.y); } print() { console.log(this.x); } } class Child extends Parent { constructor() { super(); this.x = 2; } m() { super.print(); } } let c = new Child(); c.printParent() // 10 cm() // 2 2.2 _proto_ and prototype When you first learn
The following are some built-in objects with prototype: According to the above description, look at the following code var obj = {} // equivalent to var obj = new Object() // obj.__proto__ points to the prototype of the Object constructor obj.__proto__ === Object.prototype // true // obj.toString calls the method inherited from Object.prototype obj.toString === obj.__proto__.toString // true // Array var arr = [] arr.__proto__ === Array.prototype // true For function Foo(){} var f = new Foo(); f.__proto__ === Foo.prototype // true Foo.__proto__ === Function.prototype // true 2.3 __proto__ in inheritance As a syntactic sugar for constructor functions, classes also have both
class Parent { } class Child extends Parent { } Child.__proto__ === Parent // true Child.prototype.__proto__ === Parent.prototype // true 2.4 __proto__ in inherited instances The The class Parent { } class Child extends Parent { } var p = new Parent(); var c = new Child(); c.__proto__ === p.__proto__ // false c.__proto__ === Child.prototype // true c.__proto__.__proto__ === p.__proto__ // true 3. Summary This is the end of this article about You may also be interested in:
|
<<: How to use docker to build redis master-slave
>>: Database index knowledge points summary
First we must understand that a TCP socket in the...
According to data analysis company Net Market Sha...
This article mainly involves solutions to problem...
On a Linux computer, there are two times, one is ...
Table of contents What is FormData? A practical e...
Use examples to familiarize yourself with the mean...
As shown below: LOCATE(substr,str) Returns the fi...
Table of contents 1. Function debounce 1. What is...
Table of contents Docker deployment Always on clu...
Table label composition The table in HTML is comp...
Disable swap If the server is running a database ...
This article shares the specific code of jQuery t...
Achieve results Implementation Code html <div ...
Table of contents Overview 1. Develop the require...
Since its launch in 2009, flex has been supported...