나는 그런 예가있다.프로토 타입에서 생성자 함수를 변경하는 것이 왜 불가능합니까?
function Rabbit() {
var jumps = "yes";
};
var rabbit = new Rabbit();
alert(rabbit.jumps); // undefined
alert(Rabbit.prototype.constructor); // outputs exactly the code of the function Rabbit();
은 내가 var jumps
공개 될 정도로 Rabbit()
의 코드를 변경하고 싶습니다. 나는 이렇게한다 :
Rabbit.prototype.constructor = function Rabbit() {
this.jumps = "no";
};
alert(Rabbit.prototype.constructor); // again outputs the code of function Rabbit() and with new this.jumps = "no";
var rabbit2 = new Rabbit(); // create new object with new constructor
alert(rabbit2.jumps); // but still outputs undefined
왜 이런 식으로 생성자 기능의 코드를 변경할 수 없습니까?
당신의 두 번째 코드는 거짓, 따라서 오류로 평가하는 래빗 점프하지 않습니다 말한다 -'this.jumps = "예"' – wheresrhys
@wheresrhys을하려고 모든 비어 있지 않은 문자열 (큰 길이 즉, 문자열 0보다) JavaScript에서 true로 평가됩니다. – Max