is strict mode. Internally, es6 classes and modules default to strict mode, so there is no need to use "use strict" to specify the running mode; as long as the code is written in the class or module, only strict mode is available. Considering that all future code will actually run in modules, ES6 actually upgrades the entire language to strict mode.
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
Detailed explanation of Class class in es6
class basic syntax
JavaScript language , the traditional method of generating instance objects is through the combination pattern of constructors and prototypes. ES6 provides a writing method that is closer to the traditional language (java) and introduces the concept of Class as a template for objects. Classes can be defined through the class keyword.
class point{ constructor(x,y){ this.x=x; this.y=y; } play(){ console.log("我会玩"); } }
ES6 class can be regarded as just a syntactic sugar. Most of its functions can be achieved by ES5. The new class writing method only makes the object prototype writing method clearer and more like object-oriented programming. Just grammar.
Note: "Syntactic sugar": is a term invented by British computer scientist Peter J. Landin, which refers to a certain syntax added to a computer language. This syntax is beneficial to the language. The function has no impact, but it is more convenient for programmers to use.
Several core points to note about ES6 classes and ES5 writing methods: The constructor Point of ES5 corresponds to the constructor method of the Point class of ES6. All methods of a class are defined on the prototype attribute of the class. When defining a "class" method, there is no need to add the keyword function in front of it, just put the function definition directly in it. There is no need to separate the methods with commas. If added, an error will be reported. The usage method of ES6's class is exactly the same as that of ES5's constructor
//类的所有方法都定义在类的prototype属性上面。 class piont{ constructor(){ // } play(){ } } //上述代码等价于 point.prototype={ constructor() { }, play(){ }; } //在类的实例上面调用方法,其实就是调用原型上的方法。 class Ba{ // } let b=new Ba(); b.constructor===Ba.prototype.constructor//true
In addition: ES5's constructor Point corresponds to the constructor of ES6's Point class.
Since the methods of the class are defined on the prototype object, new methods of the class can be added on the prototype object. The Object.assign method makes it easy to add multiple methods to a class at once.
class ponit{ constructor(){ } } Object.assign(Point.prototype,{ play(){ }; }) //Class直接定义的方法之间不需要逗号分隔,加了会报错. 但是这里是Object.assign的方法格式, 这里面需要往Point.prototype里面添加的方法就需要符合对象的默认格式
All methods defined inside the class are non-enumerable. Methods added to the prototype of a class through the Object.assign method, constructor cannot be enumerated, others can be enumerated
Constructor method of the basic syntax of Class
It is the default method of the class. This method is automatically called when an object instance is generated through the new command. A class must have a constructor method. If not explicitly defined, an empty constructor method will be added by default.
The constructor method returns the instance object (ie this) by default, and you can specify to return another object (The settings must be defined when creating the class. After the class is created, the return value of the constructor cannot be changed through Object.assign).
The basic syntax of Class Class calling method
The class must be called using new, otherwise an error will be reported. This is a major difference from ordinary constructors (ordinary constructors can be used as ordinary functions), which can be executed without new.
The basic syntax of Class getter and setter
is the same as ES5, in " You can use the get and set keywords inside "class" to set the value storage function and the value function for a certain attribute to intercept the access behavior of the attribute.
class demo{ constructor(age){ this.age=agie; this._age=age; } get age(){ return this._age; } set age(value){ this._age=value; console.log("年龄"+value); } } let kevin=new demo(9); kevin.age=18; console.log(kevin.age);
Class’s basic syntax and other attribute names
In the above code, The method name of the Square class, getArea, is obtained from the expression.
Special attention points on the basic syntax of Class
(1) Strict mode
The default is strict mode inside classes and modules, so it is not You need to use use strict to specify the running mode. As long as your code is written in a class or module, only strict mode is available. Considering that all future code will actually run in modules, ES6 actually upgrades the entire language to strict mode.
(2) There is no promotion
new foo(); class foo{};
In the above code, the Foo class is used first and defined later. This will cause an error because ES6 will not promote the class declaration to the head of the code. .
(3) name attribute
class point{ } point.name//point
Since in essence, the ES6 class is just a layer of packaging for the ES5 constructor, so many features of the function are inherited by Class, including the name attribute.
(4) If this points to the method of the
class, if it contains this, it will point to the instance of the class by default. However, you must be very careful, as this method may cause errors if used alone.
printName方法中的this,默认指向Logger类的实例。但是,如果将这个方法提取出来单独使用,this会指向该方法运行时所在的环境(由于 class 内部是严格模式,所以 this 实际指向的是undefined),从而导致找不到print方法而报错。
解决办法:
一个比较简单的解决方法是,在构造方法中绑定this,这样就不会找不到print方法了。
另一种解决方法是使用箭头函数。箭头函数位于构造函数内部,它的定义生效的时候,是在构造函数执行的时候。这时,箭头函数所在的运行环境,肯定是实例对象,所以this会总是指向实例对象。
class Logger{ constructor(){ this.printName=this.printName.bind(this); //但是请注意bind之后返回的函数里面的this就永久锁死了问题:!!! !!! 坚决别用 } } //箭头函数 class Obj{ constructor(){ this.getThis=()=>this; } } let o=new Obj(); o.getThis()===o//true
Class的静态属性和方法
类相当于实例的原型,所有在类中定义的方法,都会被实例继承。如果在一个方法前,加上static关键字,就表示该方法不会被实例继承,而是直接通过类来调用,这就称为“静态方法”。
class Person{ static sum=0; constructor(){ this.add(); } add(){ Person.sum++; } } let kaiwen=new Person(); console.log("当前的聊天室人数为:"+Person.sum); //作用:当没有实例化的时候,我们可以通过静态的属性和方法去获取一些信息 // 注意,如果静态方法包含this关键字,这个this指的是类,而不是实例。静态方法可以与非静态方法重名。
父类的静态方法,可以被子类继承静态方法也是可以从super对象上调用的。
class Person{ constructor(name){ this.name=name; this.sex="男"; } } class Student extends Person{ constructor(name,age){ super(name); this.age=age; } } let s=new Student("张三",11); console.log(s.name); console.log(s.age); console.log(s.sex);
Class的私有方法和私有属性
私有方法和私有属性:是只能在类的内部访问的方法和属性,外部不能访问。 这是常见需求,有利于代码的封装,但 ES6 不提供,只能通过变通方法模拟实现。
_bar方法前面的下划线,表示这是一个只限于内部使用的私有方法。但是,这种命名是不保险的,在类的外部,还是可以调用到这个方法
下面代码中的写法不仅可以写私有属性,还可以用来写私有方法
class Cat{ #eyes="眼睛"; static pai(){ console.log("凯文"); } say(){ Cat.pai(); console.log("猫有一双大大的"+this.#eyes); } } let kate=new Cat(); kate.say();
私有属性也可以设置 getter 和 setter 方法。
私有属性不限于从this引用,只要是在类的内部,实例也可以引用私有属性。
构造函数的新属性
ES6 为new命令引入了一个new.target属性,该属性一般用在构造函数之中,返回new命令作用于的那个构造函数。如果构造函数不是通过new命令调用的,new.target会返回undefined,因此这个属性可以用来确定构造函数是怎么调用的。
私有属性也可以设置 getter 和 setter 方法。
私有属性不限于从this引用,只要是在类的内部,实例也可以引用私有属性。
【相关推荐:javascript视频教程、编程视频】
The above is the detailed content of Are es6 classes in strict mode?. For more information, please follow other related articles on the PHP Chinese website!

React is a JavaScript library developed by Meta for building user interfaces, with its core being component development and virtual DOM technology. 1. Component and state management: React manages state through components (functions or classes) and Hooks (such as useState), improving code reusability and maintenance. 2. Virtual DOM and performance optimization: Through virtual DOM, React efficiently updates the real DOM to improve performance. 3. Life cycle and Hooks: Hooks (such as useEffect) allow function components to manage life cycles and perform side-effect operations. 4. Usage example: From basic HelloWorld components to advanced global state management (useContext and

The React ecosystem includes state management libraries (such as Redux), routing libraries (such as ReactRouter), UI component libraries (such as Material-UI), testing tools (such as Jest), and building tools (such as Webpack). These tools work together to help developers develop and maintain applications efficiently, improve code quality and development efficiency.

React is a JavaScript library developed by Facebook for building user interfaces. 1. It adopts componentized and virtual DOM technology to improve the efficiency and performance of UI development. 2. The core concepts of React include componentization, state management (such as useState and useEffect) and the working principle of virtual DOM. 3. In practical applications, React supports from basic component rendering to advanced asynchronous data processing. 4. Common errors such as forgetting to add key attributes or incorrect status updates can be debugged through ReactDevTools and logs. 5. Performance optimization and best practices include using React.memo, code segmentation and keeping code readable and maintaining dependability

The application of React in HTML improves the efficiency and flexibility of web development through componentization and virtual DOM. 1) React componentization idea breaks down the UI into reusable units to simplify management. 2) Virtual DOM optimization performance, minimize DOM operations through diffing algorithm. 3) JSX syntax allows writing HTML in JavaScript to improve development efficiency. 4) Use the useState hook to manage state and realize dynamic content updates. 5) Optimization strategies include using React.memo and useCallback to reduce unnecessary rendering.

React's main functions include componentized thinking, state management and virtual DOM. 1) The idea of componentization allows splitting the UI into reusable parts to improve code readability and maintainability. 2) State management manages dynamic data through state and props, and changes trigger UI updates. 3) Virtual DOM optimization performance, update the UI through the calculation of the minimum operation of DOM replica in memory.

The advantages of React are its flexibility and efficiency, which are reflected in: 1) Component-based design improves code reusability; 2) Virtual DOM technology optimizes performance, especially when handling large amounts of data updates; 3) The rich ecosystem provides a large number of third-party libraries and tools. By understanding how React works and uses examples, you can master its core concepts and best practices to build an efficient, maintainable user interface.

React is a JavaScript library for building user interfaces, suitable for large and complex applications. 1. The core of React is componentization and virtual DOM, which improves UI rendering performance. 2. Compared with Vue, React is more flexible but has a steep learning curve, which is suitable for large projects. 3. Compared with Angular, React is lighter, dependent on the community ecology, and suitable for projects that require flexibility.

React operates in HTML via virtual DOM. 1) React uses JSX syntax to write HTML-like structures. 2) Virtual DOM management UI update, efficient rendering through Diffing algorithm. 3) Use ReactDOM.render() to render the component to the real DOM. 4) Optimization and best practices include using React.memo and component splitting to improve performance and maintainability.


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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Notepad++7.3.1
Easy-to-use and free code editor