Class definition
ES6 Complete study Ruan Teacher's ECMASCRIPT6 introduction.
The general level of technology is limited, there is nothing wrong, I hope you correct.
The process used to define a constructor using the ES5 standard is as follows:
function Person (name,age) { this. Name = name; this. Age = Age ; } function () { console.log ("Hello, I am" +this. Name) } function( { Console.log ("age" + this. age+ "a pupil!) "); }
Normally capitalized functions are called constructors (not a syntax constraint, just a rule of law), attributes are written in methods, functions are written on prototypes, so that objects that instantiate (new operations) have both properties and methods.
ES6 For more clarity the concept of a constructor has a class syntax that will help us do the above, and we can think of it as a transformation of the constructor, which we usually call class. There are two ways to declare a similar function in JS:
class declaration:
Class person{ }
Class expression:
var Person = Class {}
Now let's use the class to deform the starting constructor:
class person{ Constructor (name,age) { this. Name = name; this. Age = Age ; } Say () { console.log ("Hello, I am" +this. name); } Show () { Console.log ("age" +this. age+ "a pupil! "); } }
We instantiate the object of a person, which can be used normally:
var New Person ("ZT", at all); Me.say (); Me.Show ();
The original constructor is now a class, constructor is the constructor to initialize the parameters of the deformation, say and show is the constructor function prototype above.
A class is a general term for something with the same characteristics, which can only include functions in the JS class, and cannot contain anything else, if we need to add an attribute to the class only through the Get/set accessor method:
class person{ Constructor (name,age) { this. Name = name; this. Age = Age ; } Get Message () { return ' name: ' +this. name+ ', Age: ' +this. } } var New Person ("ZT", at all); Console.log (me.message);
The constructor function can have at most one of the classes, its main function is to initialize the properties, when the new operation is performed by the constructor function to set the parameters to the properties of the object, if there is no need to have initialization properties then constructor can be omitted.
function decoration
functions defined within a class can be modified by modifiers the most common is static.
class person{ Constructor (name,age) { this. Name = name; this. Age = Age ; } Static say () { console.log ("Called by Class"); } } Person.say ();
Once a function is modified by static, the function belongs to the class and can be called directly by the class name Person.say (). While ordinary functions can not be called directly by the class, the ordinary function can only be called by the instantiated object, the static modified function is not instantiated by the object can only be called by the class directly, this function is equivalent to our previous direct use of Person.fn = function () {} Define tool functions.
Class inheritance
A class can inherit a class, the inherited class is generally referred to as the parent class, and the other is called a subclass, implemented by extends:
class person{ Constructor (name,age) { this. Name = name; this. Age = Age ; } Static say () { console.log ("I am" +this. name); } } Class Student extends person{ }
The newly created student class is a subclass, the person class is the parent class, and the subclass has all the functions within the parent class (constructor and other functions), and the subclasses inherit functions that can be used directly:
var New Student ("ZT",Stu.say) ;
The subclass does not declare any functions, and can still call Say,say through inheritance.
Subclasses can define their own unique functions, and if they have the same name as the parent function, then the function of the parent class will not take effect but the function of the subclass itself (that is, the ES5 prototype chain lookup):
class person{Constructor (name,age) { this . Name = name; this . Age = age; } say () {Console.log ( "I Am" +this .name) ; }} class Student extends person{say () {Console.log ( "I am the say function of the subclass!) ")} fn () {Console.log (" I am a subclass function fn " var stu = new Student ("Asaszt", 23 // I'm the say function of a subclass! Stu.fn (); // I am the subclass function fn
Using Super in Subclasses
Subclasses inherit the constructor function of the parent class to initialize their own properties, as well as to add their own properties, but you must use super to do this:
class person{ Constructor (name,age) { this. Name = name; this. Age = Age ; } } Class Student extends person{ Constructor (name,age,sex) { super (name,age); this. Sex = sex; } } var New Student ("ZT", 23, "male") Console.log (stu.sex); // male
Use constructor in subclasses to initialize properties, first use Super to initialize an inheritable property, and then add its own properties through this, which only exists after calling super ().
Super can also call a non-static function of the parent class (at which point we can think of super as an object instantiated by a parent class):
class person{ Constructor (name,age) { this. Name = name; this. Age = Age ; } Say () { console.log ("I am the say function of the parent class");} } Class Student extends person{ Constructor (name,age,sex) { super (name,age); this. Sex = sex; } Say () { super.say (); Console.log ("I am the say function of the subclass");} } var New Student ("ZT",Stu.say) ; // I am the say function of the parent class I am the say function of the subclass
Es6--class Basic Use