0
다음은 자바 스크립트에서 당황한 부분입니다. 아래 코드에서 하나의 속성을 포함하는 개체 생성자를 얻었습니다. 그 아래에 프로토 타입 메소드를 추가합니다. 'method1'메서드는 'this.property'에 액세스 할 수 있으며 값 30을 반환합니다. 'combine'메서드는 'method1'메서드를 호출하지만 NaN을 반환합니다. 'this.property'는 첫 번째 호출에서는 공개되었지만 두 번째 호출에서는 공개되지 않은 것 같습니다. 왜이 이상한 행동?js : 메서드가 속성에 액세스 할 수 없습니다.
var ObjBuilder = function()
{
this.property = 3;
};
ObjBuilder.prototype = function()
{
var method1 = function()
{
return this.property * 10;
}
var combine = function()
{
return method1() + 2;
}
return {method1: method1,
combine: combine};
}();
// instantiate an object and call its methods
var obj = new ObjBuilder();
console.log(obj.method1());//prints 30
console.log(obj.combine());//prints NaN. WHY???
'method1' 안에'this'를 기록한 다음'combine' 안에 호출하면 문제가 나타납니다. – Li357
@ t.niese [그것은 좋아 보이지 않습니다] (https://jsfiddle.net/3ovc6Lye /) -'창'. 'this' 문맥을주기 위해 this.method1()로 호출해야합니다. – Li357