Javascript 从内部方法设置对象变量

Javascript 从内部方法设置对象变量,javascript,oop,variables,object,set,Javascript,Oop,Variables,Object,Set,我陷入了这个问题,似乎无法为创建的对象变量指定新值。见下文: // Vesselposition class function vessel(name,ajaxName,dataUrl,pointLimit,polylineColor,iconUrl) { this.name = name; this.ajaxName = ajaxName; this.dataUrl = dataUrl; this.pointLimit = pointLimit; thi

我陷入了这个问题,似乎无法为创建的对象变量指定新值。见下文:

// Vesselposition class
function vessel(name,ajaxName,dataUrl,pointLimit,polylineColor,iconUrl) {
    this.name = name;
    this.ajaxName = ajaxName;
    this.dataUrl = dataUrl;
    this.pointLimit = pointLimit;
    this.polylineColor = polylineColor;
    this.iconUrl = iconUrl;

// Global variables
this.lat=0;
this.lng=0;
this.latlng;
this.dateTime, this.vesselIcon, this.marker, this.polyline, this.localTemp, this.localWindSpeed, this.localWindDir;
this.countryName, this.countryCode, this.localTime, this.localSunrise, this.localSunset, this.countryFlag;
this.localTemp, this.localWindSpeed, this.localWindDir, this.myOptions, this.ib;

// Function gets position data 
this.getData = function() {
    $.when(
        $.getJSON(this.dataUrl, { vessel: this.ajaxName, limit: this.pointLimit })

    ).done(function (data){
        this.path = [];
        // Create vessel icon for marker
        this.vesselIcon = new google.maps.MarkerImage(this.iconUrl,
            // This marker is 60 pixels wide by 58 pixels tall.
            new google.maps.Size(60, 58),
            // The origin for this image is 0,0.
            new google.maps.Point(0,0),
            // The anchor for this image is centered at 30,29 pixels.
            new google.maps.Point(30, 29)
        );

        if (data.markers.length < 1) {
            document.getElementById("map_canvas").innerHTML = "<h2>There was a problem obtaining vessel data, wait a couple of minutes and refresh your browser!</h2>";
        } else {
            for(i=0;i<data.markers.length;i++) {
                // Assign lat,lng, id, dateTime and heading
                this.lat = data.markers[i].marker.lat;
                this.lng = data.markers[i].marker.lng;
//Vesselposition类
功能容器(名称、ajaxName、数据URL、点限制、多段线颜色、iconUrl){
this.name=名称;
this.ajaxName=ajaxName;
this.dataUrl=dataUrl;
this.pointLimit=pointLimit;
this.polylineColor=polylineColor;
this.iconUrl=iconUrl;
//全局变量
这个。lat=0;
这是lng=0;
这是拉特林;
this.dateTime、this.vesselIcon、this.marker、this.polyline、this.localTemp、this.localWindSpeed、this.localWindDir;
this.countryName、this.countryCode、this.localTime、this.localSunrise、this.localSunset、this.countryFlag;
this.localTemp、this.localWindSpeed、this.localWindDir、this.myOptions、this.ib;
//函数获取位置数据
this.getData=函数(){
美元。什么时候(
$.getJSON(this.dataUrl,{vesser:this.ajaxName,limit:this.pointLimit})
).完成(功能(数据){
this.path=[];
//为标记创建容器图标
this.vesselIcon=new google.maps.MarkerImage(this.iconur,
//此标记宽60像素,高58像素。
新google.maps.Size(60,58),
//此图像的原点为0,0。
新google.maps.Point(0,0),
//此图像的定位点以30,29像素为中心。
新谷歌地图点(30,29)
);
如果(data.markers.length<1){
document.getElementById(“map_canvas”).innerHTML=“获取船只数据时出现问题,请等待几分钟,然后刷新浏览器!”;
}否则{
对于(i=0;i试试这个:

//keep a reference to this
var self = this;

// Function gets position data 
this.getData = function() {
....

   //use self here
   self.lat= 

我找到了一个解决方案。通过使用jQuery的代理函数..我让它工作了

// Function gets position data 
this.getData = function() {
    $.when(
    $.getJSON(this.dataUrl, { vessel: this.ajaxName, limit: this.pointLimit })

    ).done($.proxy(function (data){
    this.path = [];

    ),this}

使用与第一次提供的相同的类变量..关键是在作用域为“this”的.done函数中使用$.proxy方法。

运气不好,尝试您的建议后没有任何更改..无论如何谢谢!:)没有其他人知道吗?我不明白,它应该起作用。您将“var self=this”放在哪里了