weixin_33697898 2011-04-20 17:03 采纳率: 0%
浏览 11

jQuery Ajax和对象字段

I have this code:

MyObject = function(){
    this.field = null;

    this.build = function(){
        var my_url = "...";
        var my_data = "...";
        $.get(my_url, my_data, function(result){
           this.field = result;
        });
    }

    this.get = function(){
        return this.field;
    }
}

object = new MyObject();
object.build();
my_result = object.get() 

my_result now is null because when the inner function is executed this is not my object.
So, how can i set this.field with the $.get returned value???

  • 写回答

3条回答 默认 最新

  • weixin_33736048 2011-04-20 17:10
    关注

    In the top of your constructor, do:

    var self = this;
    

    this creates a new scoped variable that refers to the current object.

    Then in the AJAX call back do:

    self.field = result;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发