ES6 + class 에서 코드 의 진짜 모습
1. class 로 빈 클래스 를 ES6 에 정의 합 니 다.
class Person {
}
ES5 에서:
var Person = (function () {
function Person() {
}
return Person;
}());
결론: 이 결 과 는 매우 뚜렷 하 다. 원래 ES6 의 class 류 는 ES5 에서 도 구조 함 수 를 정의 한 다음 에 되 돌 아 왔 다.
2. ES6 에서 속성 정의:
class Person {
constructor(name,age) {
this.name = name; // constructor
}
age = age; //
}
ES5 에서:
var Person = (function () {
function Person(name, age) {
this.age = age; // es6 ,
this.name = name;
}
return Person;
}());
결론:
ES6
에서 정 의 된 속성 은 class
에서 직접 정의 하 든 constructor
에서 정의 하 든 모두 가능 하지만 constructor
에서 속성 을 정의 하 는 것 이 야 말로
이다. 왜냐하면 ES6 전 ES5 도 100% 합 리 적 이지 않 기 때문이다.3. 정의 방법 은 ES6 (전통 적 인 쓰기, 화살표 함수 쓰기) 에 있 습 니 다.
class Person {
Run() { //
this;
}
eat = () => { //es6
this;
}
}
ES5 에서:
var Person = (function () {
function Person() {
var _this = this;
this.eat = function () { // Person this
_this;
};
}
Person.prototype.Run = function () { // prototype
this;
};
return Person;
}());
결론: 여기 서 방법의 정 의 를 보 여 주 었 을 뿐만 아니 라
ES6
에서 전통 함수 와 화살표 함수 의 차이 도 비교 했다. 전통 함수 의 this
와 es5
함수 의 this
가 가리 키 는 것 은 똑 같 지만 화살표 함수 의 this
가 가리 키 는 것 은 외부 대상 의 역할 영역 이다. 여기 서 자세히 말 하지 않 고 여 기 를 클릭 하 십시오.4. static 키 워드 는 ES7 에서:
class Person {
static name = "Jack"; // static
static run() { // static
console.log(22)
}
static see = () => { // static
}
};
ES5 에서:
var Person = (function () {
function Person() {
}
Person.run = function () {
console.log(22);
};
return Person;
}());
Person.name = "Jack";
Person.see = function () {};
결론: class 에서 속성 이나 방법 전에 static 키 워드 를 사용 하면 외부 에서 접근 할 수 있 습 니 다. 접근 방식 은 Person. xxx 또는 Person. xxx () 입 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
전단 자동화 워 크 플 로 의 hooks예 를 들 어 우 리 는 git commt 전에 eslint 코드 검사, npm install 전에 프로젝트 의존 도 를 검사 하고 싶 습 니 다.전형 적 인 상황 에서 각종 도 구 는 특정한 동작 이 발생 할 때 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.