【问题标题】:Declare a javascript object. Then set properties with jQuery and Ajax声明一个 javascript 对象。然后用 jQuery 和 Ajax 设置属性
【发布时间】:2012-01-07 10:32:41
【问题描述】:

我无法访问实例化类的属性。该属性是使用 AJAX 调用设置的。

我正在尝试定义类“CurrentUser”,然后使用 AJAX 设置属性“userId”。


这里我定义了 CurrentUser 类,并赋予它 userID 属性:

function CurrentUser() {
    // Do an ajax call to the server and get session data.
    $.get("../../build/ajaxes/account/get_user_object_data.php", function(data) {
         this.userId = data.userId;
         console.log(data.userId);   // This will correctly output "1".
    }, "JSON");
}


这里我实例化了一个名为 billybob 的 CurrentUser。请注意我是如何无法输出 billybob 的属性:

// Instantiate the user. 
   var billybob = new CurrentUser();
   console.log(billybob.userId);     // This will incorrectly ouput "undefined".



我检查了 AJAX 的常见错误:

  • AJAX 调用以 JSON 对象的形式正确返回数据。我可以在 Firebug / 网络控制台中读取正确的对象。 AJAX 调用的状态也为“200”和“OK”。

  • 我可以正确记录 AJAX 调用的数据,如我记录 data.userId 的代码的第一部分所示。

【问题讨论】:

  • 我在想,也许“this.userId”不是指对象,而是指 AJAX 调用?
  • 即使this 引用了您想要的内容,new CurrentUser() 也会在 Ajax 方法之前返回。

标签: javascript jquery ajax object attributes


【解决方案1】:

也许这可以清除它:

在您的原始代码中:

function CurrentUser() {
    // Do an ajax call to the server and get session data.
    $.get("../../build/ajaxes/account/get_user_object_data.php", function(data) {
     this.userId = data.userId;
     console.log(data.userId);   // This will correctly output "1".
    }, "JSON");
}

您正在动态创建一个匿名函数,稍后将由 jQuery 内部调用 this 设置为 ajax 对象。所以this 将是匿名函数中的ajax 对象,而不是billybob。所以当 this.userId = ... this 表示没有 userid 属性的 ajax 对象。

jQuery 不知道你的回调函数是从哪里得到的,所以它不能自动设置this 给你。

您必须做的是保存billybob(或任何CurrentUser 实例)引用并在回调中使用它,如下所示:

function CurrentUser() {
var self = this;
    $.get("../../build/ajaxes/account/get_user_object_data.php", function(data) {
     self.userId = data.userId; //self refers to what this refered to earlier. I.E. billybob.
     console.log(data.userId, self.userid);   // This will correctly output "1".
    }, "JSON");
}

还要注意:

 var billybob = new CurrentUser();
   console.log(billybob.userId);   

当您调用console.log(即在创建billybob 后立即)时,ajax 请求尚未完成,所以它是undefined

【讨论】:

  • 不幸的是仍然不起作用:function CurrentUser() { var self = this; $.get("../../build/ajaxes/account/get_user_object_data.php", function(data) { self.userId = data.userId; //self refers to what this refered to earlier. I.E. billybob. console.log(data.userId, self.userId); // This will correctly output "1". }, "JSON"); console.log(self.userId); }
  • 在 $.get 之外,self.userId 仍然是未定义的。在设置 self.userId = data.userId 之前是否需要将 self.userId 声明为全局或其他内容?
  • @DonnyP,您是否在控制台中看到:"1 1" 而不仅仅是 "1"?然后它的工作。 console.log(billybob.userId) 因我在回答中概述的原因而不起作用。不,您只需要在回答完成后尝试登录即可。在 $.get 回调中,您可以调用另一个记录它或其他内容的函数。因为当调用该函数时,您肯定知道请求已完成。
  • 啊,我明白了。所以这也解释了为什么我的控制台在我的其他未定义的控制台日志(脚本 ln.30 和脚本 ln.55)之后记录 AJAX 调用(脚本 ln.10)。好的,让我检查一下 - 谢谢。
  • @DonnyP 下面是一个简单的例子:jsfiddle.net/4btEN/2。这实际上是同一件事,在响应准备好之前,您不能对用户做任何事情。
【解决方案2】:

在构造函数中,考虑做

var self = this;

在函数中

self.userId = data.userId;

this 内部函数与外部函数不同。虽然,我不知道 JQuery。可能它应该自动设置关闭。

【讨论】:

  • 使用“self.userId”和“this.userId”有什么区别?我习惯于 PHP,其中 self 用于静态属性,而 this 用于非静态属性。不幸的是,似乎也没有改变结果。
  • 在 Javascript 中 this 的含义与在其他语言中不同。它定义了一个范围而不是特定的对象。因此,在函数中它不会引用您正在创建的对象。但是如果你在this 仍然指向正确的对象时将它分配给变量,你可以稍后使用这个变量。这对我来说是最棘手的 JS 概念之一(我自己是 JS 新手)。
  • @AlexGitelman, this 没有定义范围或类似的东西,您可以阅读my answer here 以获得更好的理解。一点都不神奇,只是对对象的引用(非严格模式下)
  • @Esailija 正如我所说,我是 JS 新手。这并不神奇 - 对于新人来说这很棘手。我说错了范围,它只是突出了这个概念的复杂性。我被解释this 的一种方式是它是在调用函数或全局对象时在. 之前的对象。您推荐的答案非常好,感谢您指出。我的回答是针对特定问题的正确解决方案。我希望你同意。
  • @AlexGitelman,对不起,我的本意不是说你错了。您提到您是 js 新手,所以我想帮助您了解有助于我理解 this 的内容。基本上只要记住this 是在实际调用函数时决定的,这很有帮助。就像在 OP 的示例中一样,他的回调函数没有在他的代码中调用,因此他无法预测 this 将在其中包含什么,除非他知道 jQuery 使用 call/apply 明确设置的 ajax 对象调用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-11
  • 1970-01-01
  • 2018-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-03
相关资源
最近更新 更多