What is a class in javascript
In JavaScript, a class is a user-defined type, also called a class type, which is a collection of groups with the same attributes and behaviors; starting from es6, you can define a class by creating the class keyword Template, for example "class class name {}".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
What is a class
In object-oriented programming, a class is a template of an object and defines the same group of objects. (also called "instances") have properties and methods that are common to all 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, to express a class in JS, a method called The syntax style of prototype-based
function 士兵(id,hp){ this.id = id this.hp = hp } 士兵.prototype = { constructor:士兵() walk:function(){ } , Shooting:function(){ } , }
In es6, the concept of class was introduced for the first time, and the template of a class was defined by creating the class keyword.
1. Create a Class in js
class Number{ }
2. Implement the constructor, instance properties and instance methods of Class
//构造方法 class Number{ //实例属性 constructor(id,age){ //this指向当前事件 this.id=id; this.age=age; } //实例方法 num(){ console.log("hh"); } } //实例化对象 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:
hh 1 2 hh 3 4
3. Static properties and static methods of Class
//构造方法 class Number{ //静态属性调用一个方法 static ppp=1; //实例属性 constructor(id,age){ //this指向当前事件 this.id=id; this.age=age; console.log(Number.ppp) } //实例方法 num(){ console.log("hh"); }} //实例化对象 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:
1 hh 1 2 1 hh 3 4
4.Class inheritance
//父类 class Father{ //构造方法不能被继承 constructor(){ console.log("我是父亲"); this.name="father" } } //子类 class Son extend Father{ //执行子类构造方法之前必须先执行父类构造方法 constructor(){ super();//执行父类构造方法 console.log("我是儿子") } } var son=new Son; console.log(son.name)
Effect display:
我是父亲 我是儿子 father
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of What is a class in javascript. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

Real-time collaborative editors have become a standard feature of modern web development, especially in various team collaboration, online document editing and task management scenarios. Real-time communication technology based on WebSocket can improve communication efficiency and collaboration effects among team members. This article will introduce how to use WebSocket and JavaScript to build a simple online collaborative editor to help readers better understand the principles and usage of WebSocket. Understand the basic principles of WebSocketWebSo
