Class Variable Alternatives in ES6
In ES5, developers often used a comfortable pattern to create classes and class variables, as demonstrated in the following code:
// ES 5 FrameWork.Class({ variable: 'string', variable2: true, init: function(){ }, addItem: function(){ } });
However, in ES6, creating classes natively doesn't include the option to have class variables, as shown below:
// ES6 class MyClass { const MY_CONST = 'string'; // <-- this is not possible in ES6 constructor(){ this.MY_CONST; } }
This is because ES6 classes are restricted to containing methods. While it's possible to set this.myVar in the constructor, it's undesirable to clutter the constructor, especially with large classes.
Several approaches have been considered to address this issue, including:
위 내용은 기본적으로 지원되지 않는 ES6 클래스의 클래스 변수를 어떻게 관리합니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!