Is the class es5 or es6?
class is a new feature of es6. In ES6, class (class) was introduced as a template for objects, and classes can be defined through the class keyword; the new class writing method makes the writing of object prototypes clearer, more like the syntax of object-oriented programming, and more understandable. Class is a new basic syntactic sugar structure in ECMAScript. Although ES6 classes appear to support formal object-oriented programming, in fact they still use the concepts of prototypes and constructors behind them, allowing object prototypes to The writing method is clearer,
#The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
class is a new feature of es6. In ES6, class (class) was introduced as a template for objects, and classes can be defined through the class keyword.
ES6 provides a writing method that is closer to traditional languages. The newly introduced class keyword has the ability to formally define classes. Class is a new basic syntactic sugar structure in ECMAScript. Although ECMAScript 6 classes appear to support formal object-oriented programming, in fact, they still use the concepts of prototypes and constructors behind them, allowing object prototypes to The writing method is clearer and more like the syntax of object-oriented programming.
1. Introduction to class
Traditional JS only has the concept of objects and does not have the concept of classes, because JS is an object-oriented language based on prototypes, prototypes The characteristic of objects is that all attributes are shared with new objects.
ES6 introduces the concept of class. Classes can be defined through the class keyword. This is an object-oriented language that is more in line with what we usually understand.
class Person{ //定义一个名为Person的类 // 构造函数,用来接受参数 constructor(x,y){ this.x = x; //this代表的是实例对象 this.y = y; } todoSome(){ //这是个类的方法,不需要加function,有多个方法也不用逗号隔开 alert(this.x + "的年龄是" +this.y+"岁"); } } export default Person;
2. Static methods and static properties
Static methods and static properties are properties and methods that use the static keyword
2.1 Static methods
static classMethod(){ console.log('123456') }
- Static methods will not be inherited by subclasses, and subclasses cannot call
- static methods in this points to the class, not an instance of the class. Therefore static methods can only be called through the class name, not through the instance
let p = new Point(); p.classMethod(); // 报错
2.2 Static properties
static prop = 1 ; // 静态属性
- Static properties cannot be inherited by subclasses, and subclasses cannot call
- Static properties can only be called through the class name, not through instances of the class
3. Class inheritance extends
- class can use the extends keyword to inherit
- ES6 inheritance, super() must be used in the subclass constructor. Because ES6 inheritance first adds the attributes and methods of the parent class instance object to this, and then calls the constructor of the subclass to modify this this
- If the subclass does not define a constructor method, super() will default Adding a
- subclass will inherit the methods and properties of the parent class, but the static methods and properties must be called through the class name of the subclass
import classtest from "./classtest"; //先引入父类 class Man extends classtest{ constructor(x,y){ //构造函数尽量与父类参数保持一致 super(); //利用super()关键字,这个必须放在子类构造函数中的第一位置 this.x = x; this.y = y; } } export default Man;
4. Class The value function getter and the value storage function setter
getter and setter are used to read and transfer values to the attributes of the class.
The value function getter and the storage function setter can customize the assignment and value behavior. When a property only has a getter and no setter, the property is a read-only property and cannot be assigned a value, nor can it be initialized for the first time. .
If the variable is defined as private (defined outside the curly braces of the class), you can only use the getter without the setter.
let data=[1,2,3,4]; //放在类外面,属于私有变量,可以只读取 class Person{ // 构造函数 constructor(x,y){ this.x = x; this.y = y; } get x(){ console.log('获得name'); return this._name; //get读取属性 } set x(x){ console.log("设置name"); this._name=x; //set给属性赋值 } get data(){ return data; //只读属性,属性返回的值只能是私有变量 } todoSome(){ alert(this.x + "的年龄是" +this.y+"岁"); } static dayin(){ alert("dayin"); } } export default Person;
How to use:
var test= new this.$myutils.classtest('haha','18'); test.x="haha3"; //改变了实例化时候的x的值 test.todoSome(); //输出:haha3的年龄是18岁。这里就已经不是实例化时候的haha了 console.log(test.data); //结果:打印[1,2,3,4]
5. Notes:
1. When defining a method in a class, do not You can add the function keyword to the method, because the constructor in JS is defined by function, separated by two.
2. Do not separate all methods with commas, otherwise an error will be reported.
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of Is the class es5 or es6?. 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

AI Hentai Generator
Generate AI Hentai for free.

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



Concepts and instances of classes and methods Class (Class): used to describe a collection of objects with the same properties and methods. It defines the properties and methods common to every object in the collection. Objects are instances of classes. Method: Function defined in the class. Class construction method __init__(): The class has a special method (construction method) named init(), which is automatically called when the class is instantiated. Instance variables: In the declaration of a class, attributes are represented by variables. Such variables are called instance variables. An instance variable is a variable modified with self. Instantiation: Create an instance of a class, a specific object of the class. Inheritance: that is, a derived class (derivedclass) inherits the base class (baseclass)

async is es7. async and await are new additions to ES7 and are solutions for asynchronous operations; async/await can be said to be syntactic sugar for co modules and generator functions, solving js asynchronous code with clearer semantics. As the name suggests, async means "asynchronous". Async is used to declare that a function is asynchronous; there is a strict rule between async and await. Both cannot be separated from each other, and await can only be written in async functions.

Steps: 1. Convert the two arrays to set types respectively, with the syntax "newA=new Set(a);newB=new Set(b);"; 2. Use has() and filter() to find the difference set, with the syntax " new Set([...newA].filter(x =>!newB.has(x)))", the difference set elements will be included in a set collection and returned; 3. Use Array.from to convert the set into an array Type, syntax "Array.from(collection)".

For browser compatibility. As a new specification for JS, ES6 adds a lot of new syntax and API. However, modern browsers do not have high support for the new features of ES6, so ES6 code needs to be converted to ES5 code. In the WeChat web developer tools, babel is used by default to convert the developer's ES6 syntax code into ES5 code that is well supported by all three terminals, helping developers solve development problems caused by different environments; only in the project Just configure and check the "ES6 to ES5" option.

In es5, you can use the for statement and indexOf() function to achieve array deduplication. The syntax "for(i=0;i<array length;i++){a=newArr.indexOf(arr[i]);if(a== -1){...}}". In es6, you can use the spread operator, Array.from() and Set to remove duplication; you need to first convert the array into a Set object to remove duplication, and then use the spread operator or the Array.from() function to convert the Set object back to an array. Just group.

jQuery is a classic JavaScript library that is widely used in web development. It simplifies operations such as handling events, manipulating DOM elements, and performing animations on web pages. When using jQuery, you often encounter situations where you need to replace the class name of an element. This article will introduce some practical methods and specific code examples. 1. Use the removeClass() and addClass() methods jQuery provides the removeClass() method for deletion

In es6, the temporary dead zone is a syntax error, which refers to the let and const commands that make the block form a closed scope. Within a code block, before a variable is declared using the let/const command, the variable is unavailable and belongs to the variable's "dead zone" before the variable is declared; this is syntactically called a "temporary dead zone". ES6 stipulates that variable promotion does not occur in temporary dead zones and let and const statements, mainly to reduce runtime errors and prevent the variable from being used before it is declared, resulting in unexpected behavior.

The map is ordered. The map type in ES6 is an ordered list that stores many key-value pairs. The key names and corresponding values support all data types; the equivalence of key names is determined by calling the "Objext.is()" method. Implemented, so the number 5 and the string "5" will be judged as two types, and can appear in the program as two independent keys.
