In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of how to use the class class of ES6 basic grammar, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will have something to gain after reading this article on how to use the class class of ES6 basic grammar. Let's take a look.
1. Basic grammar of class
In the JavaScript language, write a student class with the following code: (prototype can add properties and methods to each object)
Function Student (stuno,stuname) {this.stuno = stuno; this.stuname = stuname;} Student.prototype.stusex = ""; Student.prototype.sayHi = function () {console.log ("Hello everyone, I am" + this.stuname+ ", my student number is" + this.stuno+ ", gender:" + this.stusex ");} var stu = new Student (" 001 "," Sun WuKong "); stu.stusex =" male "; / or / / var stu = new Student () / / stu.stuno = "001"; / / stu.stuname = "Sun WuKong"; / / stu.stusex = "male"; stu.sayHi (); / / Hello everyone, I'm Sun WuKong, my student number is 001, gender: male
ES6 provides a more traditional way of writing, introducing the concept of Class:
Constructor is a constructor, which is called automatically when an object is created:
Class Student {constructor (stuno,stuname) {this.stuno = stuno; this.stuname = stuname;} sayHi () {console.log ("Hello everyone, I am" + this.stuname+ ", my student number is" + this.stuno ");}} var stu = new Student (" 001 "," Sun WuKong "); / / or / var stu = new Student () / / stu.stuno = "001"; / / stu.stuname = "Sun WuKong"; stu.sayHi (); / / Hello everyone, I'm Sun WuKong, my student number is 001
Note: in addition to class Student, the first line of the class declaration can also be written as follows:
Let Student = classlet Student = class Student II. Properties and methods of the class
Instance properties and instance methods:
Class Student {stuno = ""; stuname = ""; sayHi () / / the method here is called prototype method {console.log ("Hello, I am" + this.stuname+ ", my student number is" + this.stuno ");}} var stu = new Student (); stu.stuno =" 001 "; stu.stuname =" Sun WuKong "; stu.sayHi ()
Static properties and static methods:
Class Student {stuno = "; stuname ="; static proName = ""; / / Professional name static proIntroduce () {console.log ("Professional name:" + Student.proName);} sayHi () {console.log ("Hello everyone, I am" + this.stuname+ ", my student number is" + this.stuno ") }} Student.proName = "computer"; Student.proIntroduce (); 3. Two ways to write the example method: (also known as prototype method) class Student {sayHi () {console.log ("hi!");}} let stu = new Student (); stu.sayHi ()
Equivalent to ES5:
Function Student () {} Student.prototype.sayHi=function () {console.log ("hi!");} var stu = new Student (); stu.sayHi (); option 2: class Student {constructor () {this.sayHi = function () {console.log ("hi") } let stu = new Student (); stu.sayHi ()
Equivalent to ES5:
Function Student () {this.sayHi = function () {console.log ("hi");} var stu = new Student (); stu.sayHi ()
When two solutions conflict, the function in constructor overrides the function outside:
Class Student {sayHi () / equivalent Student.prototype.sayHi=function () {...} {console.log ("hi!");} constructor () {this.sayHi = function () / equivalent defines {console.log ("hello!") inside function } let stu = new Student (); stu.sayHi (); / / hello!
Equivalent to ES5:
Function Student () {this.sayHi = function () {console.log ("hello!");}} Student.prototype.sayHi=function () {console.log ("hi!");} var stu = new Student (); stu.sayHi (); / / hello! IV. Class attribute encapsulation
Inside the class, you can use the get and set keywords to set a store-value function and a value-taking function on an attribute to intercept the access behavior of that property.
Class Student {get stuAge () {return this._stuAge;} set stuAge (age) {if (age > = 18 & & age)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.