a1603169 / pwa-nextjs-tailwindcss-blog

blog template with simple design
https://seunghun-website.vercel.app
1 stars 0 forks source link

blog/class_this_javascript #42

Open utterances-bot opened 8 months ago

utterances-bot commented 8 months ago

Class객체에서 constructor & this

Class객체에서 constructor & this

http://localhost:3000/blog/class_this_javascript

a1603169 commented 8 months ago

JavaScript에서 생성자 함수(constructor)의 매개변수에 값을 할당하는 것은 해당 매개변수의 기본값을 설정하는 것입니다. 이 기본값은 해당 생성자를 통해 객체를 생성할 때 해당 매개변수에 값이 제공되지 않았을 경우에 사용됩니다.

예를 들어, ListNode 클래스의 생성자에서:

constructor(val = 0, next = null) {
    this.val = val;
    this.next = next;
}

이 코드에서 val = 0next = nullvalnext 매개변수의 기본값을 각각 0null로 설정합니다. 즉, 객체를 생성할 때 valnext 값이 제공되지 않으면, 자동으로 val0으로, nextnull로 설정됩니다.

다음과 같이 ListNode 객체를 생성할 수 있습니다:

이렇게 기본값을 설정함으로써, 생성자 함수의 유연성을 높이고, 객체를 생성할 때 필요에 따라 다양한 방법으로 초기화할 수 있게 됩니다.