여기에 부모 클래스 Shape
이 있고 그 자식은 Rectangle
입니다. 몇 가지 Rectangle
개체 문제가 발생하면 - 먼저 하나의 개체가 draw
이지만 다른 개체는 없습니다. 또한 다른 유형도 있습니다 (Object
대 Shape
)자바 스크립트 - 생성 된 두 번째 개체가 첫 번째 개체와 다릅니다
왜 이런 일이 발생합니까? 여기
오류입니다 그리고 여기
function Shape(center){
this.center = center;
this.angle = 0;
}
코드
index.html
<html>
<head>
<title>TODO supply a title</title>
</head>
<body>
<div>TODO write content</div>
<script type="text/javascript" src="Rect.js"></script>
<script type="text/javascript" src="Shape.js"></script>
<script type="text/javascript" src="Main.js"></script>
</body>
</html>
에게 R Shape.js
입니다 ect.jsvar Rectangle = function(center, width,height){
Shape.call(this, center);
this.mType = "Rectangle";
this.mWidth = width;
this.mHeight = height;
var prototype = Object.create(Shape.prototype);
prototype.constructor = Rectangle;
Rectangle.prototype = prototype;
};
Rectangle.prototype.draw = function() {
//for the test
console.log("Rect is drawn");
};
마다의 예를 참조하십시오. 그러나'Rectangle.prototype.draw' 만 정의했습니다. – Barmar
@Barmar 따라서 처음으로'draw'에 이르렀습니다. 그러나 생성자 함수 만 호출 한 후에 생성 되었습니까? – DanilGholtsman