声明一个JavaScript对象。然后设置属性与jQuery和Ajax [英] Declare a javascript object. Then set properties with jQuery and Ajax

查看:123
本文介绍了声明一个JavaScript对象。然后设置属性与jQuery和Ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I can't access the attribute of an instantiated class. The attribute is set using an AJAX call.

我试图定义类的currentUser,然后设置属性用户id使用AJAX。

I am trying to define the class "CurrentUser", and then set the attribute "userId" using AJAX.


在这里,我定义类的currentUser,并给它的属性,用户ID:


Here I define the class CurrentUser, and give it the attribute 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");
}


在这里,我实例化的currentUser命名billybob。请注意,我是如何不能输出billybob的属性:


Here I instantiate a CurrentUser named billybob. Notice how I can't output billybob's attribute:

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



我已经与AJAX检查常见错误:



I've checked the common errors with AJAX:

  • AJAX调用正确返回数据作为JSON对象。我可以阅读在Firebug /网络控制台正确的对象。 Ajax调用也有200的状态为OK。

  • The AJAX call returns the data correctly as a JSON object. I can read the correct object in Firebug / Network console. The AJAX call also has a status of "200" and "OK".

我可以正确登录AJAX调用的数据,在我的code中的第一部分,我登录data.userId看到。

I can log the AJAX call's data correctly, as seen in the first part of my code where I log data.userId.

推荐答案

也许这将清除出来:

在你原来的code:

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的内部与 设置为Ajax对象。因此,将Ajax对象匿名函数内部,而不是 billybob 。因此,当 您 this.userId = ... 是指不具有UserID属性的Ajax对象。

You are creating an anonymous function on the fly, that will be later called by jQuery's internals with this set to an ajax object. So this will be the ajax object inside the anonymous function, not billybob. So when you do this.userId = ... this means the ajax object which doesn't have a userid property.

jQuery将不知道你来自哪里有你的回调函数,所以它不能设置自动地 给你。

jQuery will have no idea where you got your callback function from, so it cannot set the this automagically for you.

什么,你必须做的是保存 billybob (或的currentUser )实例参考,并用它在回调像这样:

What you must do is to save the billybob (or any CurrentUser instance) reference and use it in the callback like so:

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");
}

还要注意的是:

Also note that:

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

通过调用时间的console.log (即创建后立即 billybob ),Ajax请求不是招'重新建成尚未所以它是未定义

By the time you call console.log (I.E. instantly after creating billybob), the ajax request hasn't been completed yet so it is undefined.

这篇关于声明一个JavaScript对象。然后设置属性与jQuery和Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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