Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blog/class_this_javascript #42

Open
utterances-bot opened this issue Jan 16, 2024 · 1 comment
Open

blog/class_this_javascript #42

utterances-bot opened this issue Jan 16, 2024 · 1 comment

Comments

@utterances-bot
Copy link

Class객체에서 constructor & this

Class객체에서 constructor & this

http://localhost:3000/blog/class_this_javascript

Copy link
Owner

a1603169 commented Jan 16, 2024

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

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

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

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

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

  • new ListNode()을 호출하면, val0으로, nextnull로 초기화됩니다.
  • new ListNode(5)를 호출하면, val5로, nextnull로 초기화됩니다.
  • new ListNode(5, anotherNode)를 호출하면, val5로, nextanotherNode로 초기화됩니다.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants