개체 생성자 에 메서드 추가 일반 개체에 메서드를 추가하는 것과는 다릅니다. . 일반 객체의 경우와 마찬가지로 메서드를 추가할 수 없습니다. 객체 생성자에서 메서드를 만들려면 객체 생성자 안에 추가해야 합니다.
예시
다음 예에서 메서드 가 생성자 내부에 추가되었으므로 합법적인 값을 얻었습니다.
<html>
<body>
<script>
function Business(name, property, age, designation) {
this.Name = name;
this.prop = property;
this.age = age;
this.designation = designation;
this.name = function() {
return this.Name
};
}
var person1 = new Business("Trump", "$28.05billion", "73", "President");
document.write(person1.name());
</script>
</body>
</html> 출력
Trump