예 개체를 만들고 사용하는 데는 여러 가지 방법이 있습니다. 그렇다면 왜/언제 생성자를 생성하고 클래스를 선언하고 생성자() 메서드를 사용하는 것이 더 나은가? 강사가 달라지는 것은 아니지만 나는 그를 믿지 않는다고 말했다. ECMAScript를 2015 년에 도입언제 생성자를 통해 JavaScript에서 클래스를 사용합니까?
// 1
function Grumpy(name, profile, power){
this.name = name;
this.profile = profile;
this.power = power;
}
대
// 2
class Grumpy{
constructor(name, profile, power){
this.name = name;
this.profile = profile;
this.power = power;
}
}
참조하십시오. 자바 스크립트 엔진에서 클래스는 생성자 구문으로 변환됩니다. 그래서 강사가 옳습니다. 두 가지를 결정하는 것은 선호도, 팀에서 사용하는 것, 지원해야하는 브라우저의 경우입니다. – Shilly
또는 아마도 https://stackoverflow.com/questions/36099721/javascript-what-is-the-difference-between-function-and-class –
좋은 이유는 imho, 자바 스크립트 클래스는 일반적으로 더 나은 intellisense 경험을 제공합니다 여러 코드 편집기에서 사용되는 경우 생성자보다 – Sergeon