Open utterances-bot opened 8 months ago
JavaScript에서 생성자 함수(constructor
)의 매개변수에 값을 할당하는 것은 해당 매개변수의 기본값을 설정하는 것입니다. 이 기본값은 해당 생성자를 통해 객체를 생성할 때 해당 매개변수에 값이 제공되지 않았을 경우에 사용됩니다.
예를 들어, ListNode
클래스의 생성자에서:
constructor(val = 0, next = null) {
this.val = val;
this.next = next;
}
이 코드에서 val = 0
과 next = null
은 val
과 next
매개변수의 기본값을 각각 0
과 null
로 설정합니다. 즉, 객체를 생성할 때 val
과 next
값이 제공되지 않으면, 자동으로 val
은 0
으로, next
는 null
로 설정됩니다.
다음과 같이 ListNode
객체를 생성할 수 있습니다:
new ListNode()
을 호출하면, val
은 0
으로, next
는 null
로 초기화됩니다.new ListNode(5)
를 호출하면, val
은 5
로, next
는 null
로 초기화됩니다.new ListNode(5, anotherNode)
를 호출하면, val
은 5
로, next
는 anotherNode
로 초기화됩니다.이렇게 기본값을 설정함으로써, 생성자 함수의 유연성을 높이고, 객체를 생성할 때 필요에 따라 다양한 방법으로 초기화할 수 있게 됩니다.
Class객체에서 constructor & this
Class객체에서 constructor & this
http://localhost:3000/blog/class_this_javascript