Home > Web Front-end > JS Tutorial > body text

What is class in JavaScript? how to use?

不言
Release: 2018-12-24 11:20:07
Original
9587 people have browsed it

class is a keyword used to deal with What is class in What is class in JavaScript? how to use?? how to use? classes adopted by ECMAScript 2015 (ekma script), the standard specification for What is class in What is class in JavaScript? how to use?? how to use?. Standardized by international organizations and widely adopted in modern browsers such as Google Chrome and Internet Explorer 11 and above.

What is class in What is class in JavaScript? how to use?? how to use?

#Using classes we can simply write object-oriented programming in What is class in What is class in JavaScript? how to use?? how to use?.

Let’s take a look at how to use class

The basic procedure is as follows. After the class keyword, set the variables and methods in square brackets.

class { 
  设置变量和方法
  }
Copy after login

Let’s look at a specific example

The code is as follows

<!DOCTYPE html>
<html>
  <head>
    <meta charset = "utf-8">
    <title>What is class in What is class in JavaScript? how to use?? how to use?</title>
  </head>
  <body>
    <script>
      class Person {
        constructor(name) {
          this.name = name;
        }
        say() {
          console.log("你好," + this.name + "课程!");
        }
      }
      var x = new Person("玉女心经");
      x.say();
      var y = new Person("独孤九剑")
      y.say();
    </script>
  </body>
</html>
Copy after login

The running results are as follows

What is class in What is class in JavaScript? how to use?? how to use?

Let’s analyze the above code

class Person {
  constructor(name) {
    this.name = name;
  }
  say() {
    console.log("你好," + this.name + "课程!");
  }
}
Copy after login

Use the class keyword to define the "Person" class.

In parentheses, we define a method named "constructor" and a method named "say".

var x = new Person("玉女心经");
x.say();
var y = new Person("独孤九剑")
y.say();
Copy after login

You need to use the "new" keyword to create an instance. In doing so, the "constructor" method of the class is called.

By specifying different names when the new class is completed, the strings of "Jade Girl Heart Sutra" and "Dugu Nine Swords" are output through the say method.

This article ends here. For more exciting content, you can pay attention to the relevant columns of the php Chinese website for further learning! ! !

The above is the detailed content of What is class in JavaScript? how to use?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template