What does class mean in javascript
Editor to share with you what class refers to in javascript, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
In javascript, a class is a user-defined type, also known as a class type, which is a collection of groups with the same attributes and behavior; starting with es6, you can define a template for a class by creating a class keyword, such as "class class name {}".
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
What is a class?
In object-oriented programming, a class is a template for an object (object) that defines properties and methods common to the same set of objects (also known as "instances"). A class is a user-defined reference data type, also called a class type.
We can understand that a class is a collection of groups with the same attributes and behaviors.
Classes in JS
Before ES5, a class was expressed in JS using a syntax style called prototype-based
Function soldier (id,hp) {this.id = id this.hp = hp} soldier .prototype = {constructor: soldier () walk:function () {}, Shooting:function () {},}
In es6, the concept of class is introduced for the first time, and a template of a class is defined by creating the class keyword.
1. Create a Class in js
Class Number {}
2. The construction method, instance property and instance method of implementing Class.
/ / Constructor class Number {/ / instance property constructor (id,age) {/ / this points to the current event this.id=id;this.age=age;} / / instance method num () {console.log ("hh");}} / / instantiate object var n1=new Number ("1", "2"); n1.num (1); console.log (n1.id); console.log (n1.age); var n2=new Number ("3", "4"); n2.num (2) Console.log (n2.id); console.log (n2.age)
Effect display:
Hh12hh34
3. Static properties and static methods of Class
/ / Constructor class Number {/ / static property calls a method static ppp=1;// instance property constructor (id,age) {/ / this points to the current event this.id=id;this.age=age;console.log (Number.ppp)} / / instance method num () {console.log ("hh");}} / / instantiate object var n1=new Number ("1", "2"); n1.num (1); console.log (n1.id); console.log (n1.age) Var n2=new Number ("3", "4"); n2.num (2); console.log (n2.id); console.log (n2.age)
Effect display:
1hh121hh34
4. Inheritance of class
/ / the parent class Father {/ / constructor cannot be inherited constructor () {console.log ("I am the father"); this.name= "father"}} / / the subclass class Son extend Father {/ / must execute the parent constructor constructor () {super (); / / execute the parent constructor console.log ("I am a son")}} var son=new Son;console.log (son.name)
Effect display:
I am the father and I am the son father above is "what is the meaning of class in javascript" all the content of this article, thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!