2012-08-16 7 views
0

this.pic_w, this.pic_h와 같은 메소드가있는 Pic 클래스가 있습니다. 클래스의 메서드 중 하나에서 Image 객체를 초기화하고 그 중 하나의 메서드를 다시 작성합니다. Pic의 메소드 중 하나에 있지만 Pic에서 상속하지 않는 Image 재정의 메소드에서 내 Pic 변수 (this.pic_w, this.pic_h)에 액세스하려면 어떻게해야합니까? 일반적으로이 같은 폐쇄 내의 개체에 대한 참조를 저장하여 수행Javascript의 참조 클래스 컨테이너

Pic.prototype = new DaVinci(); 
Pic.prototype.constructor = Pic; 
function Pic(canvas) { 
    this.canvas = canvas; 
    this.pic = ""; 
    this.resize_w = 200; 
    this.resize_h = 200; 
    this.pic_w; 
    this.pic_h; 
} 

... 일부 다른 방법 ...

Pic.prototype.draw = function(img_src, pos_x, pos_y) { 
    this.setPos(pos_x,pos_y); 
    this.setPic(img_src); 
    var ctx = this.setWidget(); 
    var x = this.pos_x; 
    var y = this.pos_y; 
    var img = new Image(); 
    img.src = this.pic; 
    img.onload = function() { 
     // How can I access Pic class methods and variables from here? 
      ctx.drawImage(img, x, y, this.width, this.height); 
    } 
} 
+1

일부 코드 공유. 일반적인 질문은 자동차 엔진까지 휴대 전화를 들고 정비공에게 무엇이 잘못되었는지 묻는 것과 같습니다. – Geuis

답변

1

: 다음은 클래스 변수입니다. 다음과 같이 입력하십시오 :

Pic.prototype.draw = function(img_src, pos_x, pos_y) { 
    var that = this; //Remember "this" so you can use it later 

    this.setPos(pos_x,pos_y); 
    this.setPic(img_src); 
    var ctx = this.setWidget(); 
    var x = this.pos_x; 
    var y = this.pos_y; 
    var img = new Image(); 
    img.src = this.pic; 
    img.onload = function() { 
     // Here I can access this.width, this.height values. 
     // I want to save those values as Pic.pic_w, and Pic.pic_h 
     ctx.drawImage(img, x, y, this.width, this.height); 
     that.pic_w = this.width; //Now you can set the properties of "that" 
     that.pic_h = this.height; 
    } 
    return ss; 
    // END 
} 
+0

놀라운! 정말 고마워! – JuanCB