Does class in es6 have static attributes?
Class in es6 has no static attributes. Static attributes are attributes of the class itself, that is, attributes directly defined inside the class (Class.propname) and do not need to be instantiated; however, ES6 stipulates that there are only static methods inside Class and no static attributes.
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
In ES6, class (class) was introduced as a template for objects, and classes can be defined through the class keyword.
The essence of class is function.
It can be regarded as a syntax sugar that makes the writing of object prototypes clearer and more like the syntax of object-oriented programming.
ES6 Class static methods, properties and instance properties
The class is equivalent to the prototype of the instance. All methods defined in the class will be Instance inheritance. If the static keyword is added before a method, it means that the method will not be inherited by the instance, but will be called directly through the class. This is called a "static method".
class Foo { static classMethod() { return 'hello'; } } Foo.classMethod() // 'hello' var foo = new Foo(); foo.classMethod() // TypeError: foo.classMethod is not a function
In the above code, there is the static keyword before the classMethod method of class Foo, indicating that the method is a static method and can be called directly on class Foo (Foo.classMethod()), not on class Foo Called on the instance. If a static method is called on an instance, an error is thrown indicating that the method does not exist.
Static methods of parent classes can be inherited by subclasses.
class Foo { static classMethod() { return 'hello'; } } class Bar extends Foo {} Bar.classMethod(); // 'hello'
In the above code, the parent class Foo has a static method, and the subclass Bar can call this method.
Static methods can also be called from the super object.
class Foo { static classMethod() { return 'hello'; } } class Bar extends Foo { static classMethod() { return super.classMethod() + ', too'; } } Bar.classMethod();
Static properties
Static properties refer to the properties of the Class itself, namely Class.propname, rather than the properties defined on the instance object (this).
class Foo {} Foo.prop = 1; Foo.prop // 1
The above writing method defines a static property prop for the Foo class.
Currently, only this way of writing is feasible, because ES6 clearly stipulates that there are only static methods inside Class and no static attributes.
// 以下两种写法都无效 class Foo { // 写法一 prop: 2 // 写法二 static prop: 2 } Foo.prop // undefined
ES7 has a proposal for static properties, currently supported by the Babel transcoder.
This proposal stipulates new writing methods for both instance attributes and static attributes.
(1) Instance attributes of a class
The instance attributes of a class can be written into the definition of the class using equations.
class MyClass { myProp = 42; constructor() { console.log(this.myProp); // 42 } }
In the above code, myProp is the instance attribute of MyClass. On instances of MyClass, this property can be read.
Previously, when we defined instance attributes, we could only write them in the constructor method of the class.
class ReactCounter extends React.Component { constructor(props) { super(props); this.state = { count: 0 }; } }
In the above code, the this.state attribute is defined in the constructor.
With the new way of writing, you don’t need to define it in the constructor method.
class ReactCounter extends React.Component { state = { count: 0 }; }
This way of writing is clearer than before.
For the purpose of readability, the new writing method allows direct listing of instance properties that have been defined in the constructor.
class ReactCounter extends React.Component { constructor(props) { super(props); this.state = { count: 0 }; } state; }
(2) Static attributes of the class
The static attributes of the class only need to be added with the static keyword in front of the instance attribute writing method above.
class MyClass { static myStaticProp = 42; constructor() { console.log(MyClass.myProp); // 42 } }
Similarly, this new writing method greatly facilitates the expression of static attributes.
// 老写法 class Foo {} Foo.prop = 1; // 新写法 class Foo { static prop = 1; }
In the above code, the static properties of the old way of writing are defined outside the class. After the entire class is generated, static attributes are generated. This makes it easy to ignore this static attribute, and does not comply with the code organization principles that related code should be put together. In addition, the new way of writing is explicit declaration (declarative) instead of assignment processing, which has better semantics.
The above is the detailed content of Does class in es6 have static attributes?. 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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.

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.

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)".

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.

When writing PHP code, using classes is a very common practice. By using classes, we can encapsulate related functions and data in a single unit, making the code clearer, easier to read, and easier to maintain. This article will introduce the usage of PHPClass in detail and provide specific code examples to help readers better understand how to apply classes to optimize code in actual projects. 1. Create and use classes In PHP, you can use the keyword class to define a class and define properties and methods in the class.
