Skip to content

Instantly share code, notes, and snippets.

@CreatiCoding
Created July 18, 2021 06:25
Show Gist options
  • Select an option

  • Save CreatiCoding/30982f0cdf265f523cdae4f0c829e54a to your computer and use it in GitHub Desktop.

Select an option

Save CreatiCoding/30982f0cdf265f523cdae4f0c829e54a to your computer and use it in GitHub Desktop.
JS 클래스와 함수
function Person(name){ this.name = name; }
Person.prototype.say = function(){ console.log(this.name); }
var creco = new Person("creco")
creco.say()
class Person2 {
constructor(name) {
this.name = name;
}
say() {
console.log(this.name);
}
}
var creco2 = new Person2("creco2");
creco2.say()
// 자바스크립트의 클래스 본질은 함수다.
// this는 호출된 곳의 주인을 가리킨다.
@CreatiCoding

Copy link
Copy Markdown
Author

클래스와 함수의 차이

  • Class constructor 클래스명 cannot be invoked without 'new'
  • 클래스는 함수처럼 호출하면 위와 같은 오류를 뿜는다.
  • 함수는 클래스처럼 new를 쓸 수도 있고 그냥 호출할 수도 있다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment