객체 생성자에 속성 추가 일반 개체에 속성을 추가하는 것과 다릅니다. . 속성을 추가하려면 생성자 에 추가해야 합니다. 생성자 외부가 아닌 자체에 있는 반면 일반 개체의 아무 곳에나 추가할 수 있습니다.
예시-1
다음 예에서 속성 일반 개체의 경우와 같이 추가됩니다. 여기에서 우리는 객체 생성자를 사용했습니다. 미정의가 아닌 경우 생성자 내부에 속성을 추가해야 합니다. 아래와 같이 출력됩니다.
<html>
<body>
<p id = "prop"></p>
<script>
function Business(name, property, age, designation) {
this.Name = name;
this.prop = property;
this.age = age;
this.designation = designation;
}
Business.language = "chinese";
var person1 = new Business("Trump", "$28.05billion", "73", "President");
var person2 = new Business("Jackma", "$35.6 billion", "54", "entrepeneur");
document.write(person2.language);
</script>
</body>
</html> 출력
undefined
예시-2
다음 예에서 속성 "언어 "는 생성자 내부에서 선언되므로 false 값과 달리 정상적인 결과를 얻습니다. .
<html>
<body>
<p id = "prop"></p>
<script>
function Business(name, property, age, designation) {
this.Name = name;
this.prop = property;
this.age = age;
this.designation = designation;
this.language = "chinese";
}
var person1 = new Business("Trump", "$28.05billion", "73", "President");
var person2 = new Business("Jackma", "$35.6 billion", "54", "entrepeneur");
document.write(person2.language);
</script>
</body>
</html> 출력
chinese