2
function Vector(i, j, k){
this.i = i;
this.j = j;
this.k = k;
};
Vector.prototype = {
addition: function(vec){
return new Vector(vec.i+this.i, vec.j+this.j, vec.k+this.k);
},
magnitude: function(){
return Math.sqrt(this.i*this.i + this.j*this.j + this.k*this.k);
},
};
function Orbit(rp, ra, ecc){
this.rp = new Vector(rp, 0, 0);
this.ra = new Vector(ra, 0, 0);
this.a = this.rp.addition(this.ra).magnitude(); //The error is in this line
};
var orbit = new Orbit(6563, 42165);
그래서 내가 여기서 할 노력하고있어 무엇 Orbit
객체에 rp
, ra
에 대한 Vector
객체를 생성합니다. a
은 벡터 rp
및 ra
의 프로토 타입 방법을 사용하는데,하지만 난 스크립트를 실행할 때, rp
의 프로토 타입 방법을 사용할 수 없습니다 내가 말하는 오류가 발생한다 :개체의 메서드를 다른 개체에서 사용할 수없는 이유는 무엇입니까?
TypeError: 'undefined' is not a function (evaluating 'this.rp.addition(this.ra)')
나는이 아닌 희망 이것이 내가 잠시 동안 약간의 문제를주고 있었기 때문에 내가 어딘가에 놓친 바보 같은 실수. JS에서 프로토 타입을 사용하는 것은 이번이 처음이기 때문에 여기서 잘못하고있는 것을 보지 못합니다. 왜 this.rp
에 Vector
메쏘드가없고 왜 내 코드를 수정해야 하는지를 알아야합니다.
'this.a'가 숫자가되어야하고 작업을 수행해야하는 작업을 변경해야합니다. 'this.rp.addition (this.ra)'는 단지 벡터를 덧붙여서 함께 추가 한 다음 그 크기를 찾는 것입니다. 콘솔에서 나는'orbit.a = orbit.rp.addition (orbit.ra) .magnitude()'를 할 수 있었기 때문에 위의 코드가 작동하지 않는 이유를 알 수 없다. – EvanA
@EvanA 문제는'this.rp.addition (this.ra) .magnitude()'를 계산하는 것이 아니라, 이후에 크기를 취하는 것에서 그것은'this.rp.addition (this.ra) .magnitude () .magnitude()' –