Get the App
SLTechnology News&Howtos  ›  Development  › 

How to declare variables with the keyword class in es6

Shulou Source: shulou.com Published: 2022-06-01 03:10:00 09月21日 Update

Editor to share with you how to use the keyword class to declare variables in es6, 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!

Declare variables with the keyword class

Let's put it briefly here: first, look at the constructor in ES5, and then use ES6's class to implement it:

/ / ES5: function fun (x, y) {this.x = x; this.y = y;}; fun.prototype.GetHair = function () {return `${this.x} lost two hairs, ${this.y} said me too! `;}; let setHair = new fun ('Xiaoming', 'Lao Wang'); console.log (setHair.GetHair ()); / / Xiaoming lost two hairs, Lao Wang said I was the same!

Let's take a look at ES6's class writing:

Class Interest {constructor (x, y, e, z) {this.x = x; this.y = y; this.e = e; this.z = z;} MyInterest () {let arr = []; console.log (`I will ${[... arr,this.x,this.y,this.e,this.z]}! `) } let GetInterest = new Interest ('sing', 'jump', 'rap',' basketball'); console.log (GetInterest.MyInterest ()); / / I can sing, dance, rap, basketball!

ES6's class can be regarded as a syntax sugar, most of its functions can be done by ES5, the new class writing only makes the object prototype written more clearly, more like the syntax of object-oriented programming, the constructor method is the construction method, and the this keyword represents the instance object. This is the constructor Point of ES5, which corresponds to the constructor of the Point class of ES6.

The above is all the content of the article "how to declare variables with the keyword class in es6". 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!

Tags: Key keyword writing variable object method article content function just basketball grammar Xiao Ming Lao Wang hair not much that is representative function prototype Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Apple Redmi OPPO Reno macOS