Class definition and basic usage

Function Person(x, x) Y) {this.x = x this.y = y} let methodsName = 'methodtest' // es6 after Class Person {// only one Class constructor // 1. Prototype = Person. Prototype = Person. Prototype // 2. Assign the object to the function's this: new bind this = moni // 4. Constructor (x, y) {this.x = x this.y = y} // These methods are defined in Person's prototype. ToString () {return '${this.x},${this.y}'} // var p = new Person() // p.eating() {console.log(this.name + "eating~")} // Can write method name [methodsName]() {} // class accessor method: {console.log(" block access operation ") return this._address} set address(newAddress) {console.log(" block setup operation ") this._address = Static methods can be called directly on the class, not on the instance. If this exists on the static method, New var p = new Person(1,2) // the Person type is a function, The implicit prototype on the p object is the same thing as the display prototype on the Person console.log(p.__proto__ === Person.prototype) // true console.log(p) // p {x:1, y:2}Copy the code

In ES6, all new instances of the same class share the same prototype object, and we can also add methods to the instance by _ proto _

__proto__ === p2.__proto__ === Person. Prototype // Example P1.__proto__. Eating = function(){return '${this.name}'} //Copy the code

Class implements extends

class Person { constructor(name, age) { this.name = name this.age = age } eating() { console.log(this.name + " eating~") } personMethod() { Log (" handle logic 1") console.log(" handle logic 2") console.log(" handle logic 3")}} // Student is called a subclass, inheriting the word Person. Class Student extends Person {// Class Student extends Person {// Class Student extends Person {// Class Student extends Person {// Class Student extends Person {// Class Student extends Person {// Class Student extends Person {// Sno) {super(name, age) constructor(name, age) Age) this.sno = sno} studying() {console.log(this.name + "studying~")} Console. log("student "+ this.name +" running")} // Rewrite the personMethod method personMethod() {// reuse the processing logic in the parent class Super.personmethod () console.log(" console.log ") console.log(" console.log ")}} var stu = new Student(" XXX ", 18, 111) stu.eating() stu.running() stu.personMethod() Student.staticMethod() console.log(Object.getOwnPropertyDescriptors(Person))Copy the code

\