Es6 of self-learning (6)

Source: Internet
Author: User

ES6 's new features add class and class inheritance, in fact this feature is also borrowed from the background language, first look at the next es5 how to construct an object

function Human () {
  this.eyes=2;
  this.hands=2;
}

Human.prototype.singing=function () {
	console.log (' I can Sing ');
}

Let A = new Human ();
Console.log (a);

A.singing ();
As above, an object called human is constructed with the ES5 method, 2 attributes are created in the object, a method is mounted in the prototype method, the instantiation of a human object is declared, and a call to singing this method is made.

Again, how to use ES6 to declare a class:

Class human{
	Constructor () {
		this.eyes=2;
		this.hands=2
	}
}

Let A = new Human ();
Console.log (a);

ES6 uses the class keyword to declare a class, using constructor to create a class constructor that initializes an object's properties, ES6 only uses a more semantic and normative way of declaring a class, but the underlying principle is based on ES5, if you want to create a method , and you don't have to mount it on the prototype to create it directly in the object.

Class human{
	Constructor () {
		this.eyes=2;
		this.hands=2;
	}

	Singing () {
		console.log (' I can Sing ');
	}

Let A = new Human ();
Console.log (a);

A.singing ();
How do you inherit a class in ES6?


Class human{
	Constructor () {
		this.eyes=2;
		this.hands=2;
	}

	Singing () {
		console.log (' I can Sing ');
	}

Let A = new Human ();
Console.log (a);

A.singing ();

Class Normalman extends Human {

} let

b=new Normalman ();
Console.log (b);

A class can inherit another class, using the extends syntax to inherit all the properties and methods of another class, and in addition, it can transform inherited properties and methods or create new properties and methods

Class human{
	Constructor () {
		this.eyes=2;
		this.hands=2;
	}

	Singing () {
		console.log (' I can Sing ');
	}

Let A = new Human ();
Console.log (a);

A.singing ();

Class Normalman extends Human {
  	constructor () {
  		super ();
		this.eyes=100;
		this.hands=200;
		this.feet=2;
	}

	Singing () {
		console.log (' Normalman can Sing ');
	}
	Run () {
		console.log (' Normalman can run ');
	}

Let B=new Normalman ();
Console.log (b);
B.singing ();
B.run ();
You can transform inherited properties in constructor, you can create your own method, super () called the constructor of the parent class, when inheriting another class must use, if you do not have to complain.

We can pass the argument in the constructor of the parent class, and the argument is invoked

Class human{
	Constructor (eyes=2,hands=2) {
		this.eyes=eyes;
		this.hands=hands;
	}

	Singing () {
		console.log (' I can Sing ');
	}

Let A = new Human (3,4);
Console.log (a);

A.singing ();

Class Normalman extends Human {
  	constructor () {
  		super ();
		this.eyes=100;
		this.hands=200;
		this.feet=2;
	}

	Singing () {
		console.log (' Normalman can Sing ');
	}
	Run () {
		console.log (' Normalman can run ');
	}

Let B=new Normalman ();
Console.log (b);
B.singing ();
B.run ();
The constructor of the parent class is invoked in super (), so super () can also be used to pass the reference

Class human{
	Constructor (eyes=2,hands=2) {
		this.eyes=eyes;
		this.hands=hands;
	}

	Singing () {
		console.log (' I can Sing ');
	}

Let A = new Human (3,4);
Console.log (a);

A.singing ();

Class Normalman extends Human {
  	constructor () {
  		super (1000,2000);
		this.feet=2;

	}

	Singing () {
		console.log (' Normalman can Sing ');
	}
	Run () {
		console.log (' Normalman can run ');
	}

Let B=new Normalman ();
Console.log (b);
B.singing ();
B.run ();

About the this point in the constructor, we can pass a name parameter in the constructor of the inherited function, and create a new name attribute in the constructor, which can be spliced in the Singing () method with the template string ${this.name}. Finally, in the instantiated Normalman (), pass in Mike, and finally print out Normalman This object has name: "Mike"

If you use another variable to receive the singing in B, and then call this method, this time and run a normal function, then print this,this for undefined

Class human{
	Constructor (eyes=2,hands=2) {
		this.eyes=eyes;
		this.hands=hands;
	}

	Singing () {
		console.log (' I can Sing ');
	}

Let A = new Human (3,4);
Console.log (a);

A.singing ();

Class Normalman extends Human {
  	constructor (name= ' Flowke ') {
  		super (1000,2000);
		this.feet=2;
        this.name=name;
	}

	Singing () {
		console.log (this);
		/*console.log (' ${this.name} can sing '); */
	}
	Run () {
		console.log (' Normalman can run ');
	}

Let B=new normalman (' Mike ');
Console.log (b);
B.singing ();
B.run ();

Let fnn=b.singing;
FNN ();
How to solve this problem, how to let this one always point to new out of this example. You can write this in the constructor constructor This.singing=this.singing.bind (this);

Class human{
	Constructor (eyes=2,hands=2) {
		this.eyes=eyes;
		this.hands=hands;
	}

	Singing () {
		console.log (' I can Sing ');
	}

Let A = new Human (3,4);
Console.log (a);

A.singing ();

Class Normalman extends Human {
  	constructor (name= ' Flowke ') {
  		super (1000,2000);
		this.feet=2;
        This.name=name;
        This.singing=this.singing.bind (this);
	}

	Singing () {
		/*console.log (this); */
		Console.log (' ${this.name} can sing ');
	}
	Run () {
		console.log (' Normalman can run ');
	}

Let B=new normalman (' Mike ');
Console.log (b);
B.singing ();
B.run ();

Let fnn=b.singing;
FNN ();
This.singing=this.singing.bind (this); the this.singing at the back refers to the singing () method, which is later bound to this, then let fnn= B.singing, here's the b.singing access is this.singing, and here This.singing has bound this point, this time to run the FNN, and this is the construction of the time class instances, this time running FNN, you will find that print out is MI Ke is also B, the instantiated object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.