使用$ .getJSON()与回调一个Javascript对象中 [英] Using $.getJSON() with callback within a Javascript object

查看:116
本文介绍了使用$ .getJSON()与回调一个Javascript对象中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个对​​象,使之具有封装$ .getJSON方法。这是我的设置:

 功能属性(价格,存款){
  this.price =价格;
  this.deposit =存款;
  this.getMortgageData =功能(){
    $ .getJSON('http://example.com/index?p='+this.price+'&d='+this.deposit+'&c=?',功能(数据){
        this.mortgageData =数据;
    });
  }
  返回true;
}
 

现在的问题似乎是,我没有获得'这个'内的getJSON回调函数,这是有道理的。

有没有解决此类型的功能或我只是想这是完全错误的方式?我只曾经真的$使用PHP的面向对象之前,所以JS OO有点新的给我C $ CD。

我试过其他的事情是:

 功能属性(价格,存款){
  this.price =价格;
  this.deposit =存款;
  this.getMortgageData =功能(){
    this.mortgageData = $ .getJSON('http://example.com/index?p='+this.price+'&d='+this.deposit+'&c=?',功能(数据){
        返回的数据;
    });
  }
  返回true;
}
 

但尽管如此,

  VAR道具=新的物业();
prop.getMortgageData();
//等待响应,然后
警报(prop.mortgageData.xyz); // ==未定义
 

解决方案

您第一次尝试接近,但正如你说的,你不能访问回调内因为它指的是别的东西。相反,在外部范围分配另一个名字,并访问。回调是一个闭包,将有机会获得该变量在外部范围:

 功能属性(价格,存款){
  this.price =价格;
  this.deposit =存款;
  VAR属性=这一点; //这个变量将在回调访问,但仍指的是正确的对象。
  this.getMortgageData =功能(){
    $ .getJSON('http://example.com/index?p='+this.price+'&d='+this.deposit+'&c=?',功能(数据){
        property.mortgageData =数据;
    });
  }
  返回true;
}
 

I'm trying to set up an object so that it has an encapsulated $.getJSON method. Here's my setup:

function Property(price, deposit){
  this.price = price;
  this.deposit = deposit;
  this.getMortgageData = function(){
    $.getJSON('http://example.com/index?p='+this.price+'&d='+this.deposit+'&c=?', function(data){
        this.mortgageData = data;
    });
  }
  return true;
}

Now the problem seems to be that I don't have access to 'this' inside the getJSON callback function which makes sense.

Is there a workaround for this type of function or am I just thinking about this totally the wrong way? I've only ever really coded using PHP OO before so JS OO is a bit new to me.

Other things I've tried are:

  function Property(price, deposit){
  this.price = price;
  this.deposit = deposit;
  this.getMortgageData = function(){
    this.mortgageData = $.getJSON('http://example.com/index?p='+this.price+'&d='+this.deposit+'&c=?', function(data){
        return data;
    });
  }
  return true;
}

But still,

var prop = new Property();
prop.getMortgageData();
// wait for the response, then
alert(prop.mortgageData.xyz); // == undefined

解决方案

Your first attempt is close, but as you said, you can't access this inside the callback because it refers to something else. Instead, assign this to another name in the outer scope, and access that. The callback is a closure and will have access to that variable in the outer scope:

function Property(price, deposit){
  this.price = price;
  this.deposit = deposit;
  var property = this; // this variable will be accessible in the callback, but still refers to the right object.
  this.getMortgageData = function(){
    $.getJSON('http://example.com/index?p='+this.price+'&d='+this.deposit+'&c=?', function(data){
        property.mortgageData = data;
    });
  }
  return true;
}

这篇关于使用$ .getJSON()与回调一个Javascript对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆