search
HomeWeb Front-endFront-end Q&AWhat is a class in javascript

What is a class in javascript

Sep 16, 2021 pm 04:47 PM
javascriptkind

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 {}".

What is a class in javascript

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
CSS: Can I use multiple IDs in the same DOM?CSS: Can I use multiple IDs in the same DOM?May 14, 2025 am 12:20 AM

No,youshouldn'tusemultipleIDsinthesameDOM.1)IDsmustbeuniqueperHTMLspecification,andusingduplicatescancauseinconsistentbrowserbehavior.2)Useclassesforstylingmultipleelements,attributeselectorsfortargetingbyattributes,anddescendantselectorsforstructure

The Aims of HTML5: Creating a More Powerful and Accessible WebThe Aims of HTML5: Creating a More Powerful and Accessible WebMay 14, 2025 am 12:18 AM

HTML5aimstoenhancewebcapabilities,makingitmoredynamic,interactive,andaccessible.1)Itsupportsmultimediaelementslikeand,eliminatingtheneedforplugins.2)Semanticelementsimproveaccessibilityandcodereadability.3)Featureslikeenablepowerful,responsivewebappl

Significant Goals of HTML5: Enhancing Web Development and User ExperienceSignificant Goals of HTML5: Enhancing Web Development and User ExperienceMay 14, 2025 am 12:18 AM

HTML5aimstoenhancewebdevelopmentanduserexperiencethroughsemanticstructure,multimediaintegration,andperformanceimprovements.1)Semanticelementslike,,,andimprovereadabilityandaccessibility.2)andtagsallowseamlessmultimediaembeddingwithoutplugins.3)Featur

HTML5: Is it secure?HTML5: Is it secure?May 14, 2025 am 12:15 AM

HTML5isnotinherentlyinsecure,butitsfeaturescanleadtosecurityrisksifmisusedorimproperlyimplemented.1)Usethesandboxattributeiniframestocontrolembeddedcontentandpreventvulnerabilitieslikeclickjacking.2)AvoidstoringsensitivedatainWebStorageduetoitsaccess

HTML5 goals in comparison with older HTML versionsHTML5 goals in comparison with older HTML versionsMay 14, 2025 am 12:14 AM

HTML5aimedtoenhancewebdevelopmentbyintroducingsemanticelements,nativemultimediasupport,improvedformelements,andofflinecapabilities,contrastingwiththelimitationsofHTML4andXHTML.1)Itintroducedsemantictagslike,,,improvingstructureandSEO.2)Nativeaudioand

CSS: Is it bad to use ID selector?CSS: Is it bad to use ID selector?May 13, 2025 am 12:14 AM

Using ID selectors is not inherently bad in CSS, but should be used with caution. 1) ID selector is suitable for unique elements or JavaScript hooks. 2) For general styles, class selectors should be used as they are more flexible and maintainable. By balancing the use of ID and class, a more robust and efficient CSS architecture can be implemented.

HTML5: Goals in 2024HTML5: Goals in 2024May 13, 2025 am 12:13 AM

HTML5'sgoalsin2024focusonrefinementandoptimization,notnewfeatures.1)Enhanceperformanceandefficiencythroughoptimizedrendering.2)Improveaccessibilitywithrefinedattributesandelements.3)Addresssecurityconcerns,particularlyXSS,withwiderCSPadoption.4)Ensur

What are the main areas where HTML5 tried to improve?What are the main areas where HTML5 tried to improve?May 13, 2025 am 12:12 AM

HTML5aimedtoimprovewebdevelopmentinfourkeyareas:1)Multimediasupport,2)Semanticstructure,3)Formcapabilities,and4)Offlineandstorageoptions.1)HTML5introducedandelements,simplifyingmediaembeddingandenhancinguserexperience.2)Newsemanticelementslikeandimpr

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool