2014-07-06 4 views
3

아래의 자바 스크립트에서 위도 (위도) 변수를 인쇄하려고합니다.다른 함수에서 변수를 사용하는 방법은 무엇입니까?

어떻게 위 변수를 참조 할 수 있습니까? this.lat, that.lat, vicinity.lat 등? 내 자바 스크립트에서

나는 당신이 거의 거기

var Vicinity = function (pubReference, sessionId, done, err) { 
    this.ad = { 
     getBannerHtml: function() { 
      console.log(this.lat); // how do I refer to the lat variable? 
     } 
    }; 

    this.init = function (done, err) { 
     var that = this; 
     this.getLatLng(that, function (position) { 
      that.lat = position.coords.latitude; 
     }, err); 
     if (typeof done === 'function') 
      done(this); 
    }; 

    this.init(done, err); 
}; 

$(document).ready(function() { 
    var data = new Vicinity(pubReference, sessionId, 
     function (result) { 
      $("#sticky").html(result.ad.getBannerHtml()); 
     } 
    ); 
}); 
+0

, 즉는 상위 페이지와 같은 도메인에, 당신은 http://stackoverflow.com/questions/17142285/access-iframe 그것을 내에서 변수에 액세스 할 수 없습니다 -content-parent-jquery –

+0

'getBannerHtml'에서'this'가'Vicinity'를 참조하거나'Vicinity'에 의해 인스턴스를 생성 한 것으로 생각된다면, this.ad 객체를 참조하는 것입니다. 그래서 이것을 이것을'that' 변수에 넣으면 더 좋을 것입니다. Look at [Demo] (http://jsfiddle.net/GKDev/U5xZ4/) – Givi

+0

함수에서 getBannerHtml : function() lat 및 lon 변수를 어떻게 참조 할 수 있습니까? 이거. 그거. 그거. 슬림, 인근. 아빠? – grabury

답변

0

있습니다. 이미 thatinit()this으로 표시됩니다. 그냥 그 함수를 통해 작동해야 할 :

var Vicinity = function (pubReference, sessionId, done, err) { 
    var that = this; 
    this.ad = { 
     getBannerHtml: function() { 
      console.log(that.lat); // how do I refer to the lat variable? 
     } 
    }; 

    this.init = function (done, err) { 
     var that = this; 
     this.getLatLng(that, function (position) { 
      that.lat = position.coords.latitude; 
     }, err); 
     if (typeof done === 'function') 
      done(this); 
    }; 

    this.init(done, err); 
}; 

$(document).ready(function() { 
    var data = new Vicinity(pubReference, sessionId, 
     function (result) { 
      $("#sticky").html(result.ad.getBannerHtml()); 
     } 
    ); 
}); 
0

는 또한 "적용"기능을 사용하여 근교의 범위에서 getBannerHtml 메소드를 호출 할 수 있습니다. "this"변수는 전달한 객체로 설정됩니다.

$(document).ready(function() { 
    var data = new Vicinity(pubReference, sessionId, 
     function (result) { 
      $("#sticky").html(result.ad.getBannerHtml.apply(result)); 
     } 
    ); 
}); 
0

이미 다른 게시물에 게시하지만 다른 생각이있어) 그것은 가지 분명하지만 작동합니다.

getBannerHtml: function(lat, lon) { 
    return '<iframe src="http://foo.com?pr=34&lat'+ lat +'=&lon=' + lon + '"></iframe>'; 
} 

$(document).ready(function() { 
    var pubReference = 1; 
    var sessionId = "1"; 
    var data = new Vicinity (pubReference, sessionId, 
    function(result) { 
     $("#sticky").html(result.ad.getBannerHtml(result.lat, result.lon)); 
    } 
); 
}); 

"적용"기능을 사용하여 해당 지역의 범위에서 getBannerHtml 메소드를 호출 할 수도 있습니다. "this"변수는 전달한 객체로 설정됩니다. 당신이하지 "소유"iFrame을 할 경우

$(document).ready(function() { 
    var pubReference = 1; 
    var sessionId = "1"; 
    var data = new Vicinity(pubReference, sessionId, 
     function (result) { 
      $("#sticky").html(result.ad.getBannerHtml.apply(result)); 
     } 
    ); 
}); 
+0

FYI : http://stackoverflow.com/questions/24594306/how-to-pass-values-to-a에서 옮겼습니다. -기능 – Shog9

관련 문제