简体   繁体   English

如何从对象获取属性

[英]How to get properties from an object

I'm using this code to retrieve a simple JSON object: 我正在使用以下代码来检索一个简单的JSON对象:

function userinfo() {
    var user = new Object();
    $.getJSON('###', function(data){
        user.id = data.user.uID;
        user.name = data.user.uname;
    });
    return user;
}
var user = userinfo();
console.log(user);

When I run console.log on the user object outside of the function, I can see the properties of the object (id, name). 在功能之外的用户对象上运行console.log时,我可以看到对象的属性(id,name)。 But when I try to console.log "user.id" or "user.name", they come up as undefined. 但是当我尝试console.log“ user.id”或“ user.name”时,它们显示为未定义。 I can't work out why this is, or what I can do to retrieve the properties without having to iterate over them. 我无法弄清楚为什么会这样,或者无法在不进行迭代的情况下检索属性。

Any suggestions? 有什么建议么?

The AJAX call to get the JSON is asynchronous, the callback function(data) isn't called until the AJAX returns, but you are reading user right after the AJAX request is sent, but before the AJAX response is received. 用于获取JSON的AJAX调用是异步的,直到AJAX返回时才调用回调function(data) ,但是您是在发送AJAX请求之后但在收到AJAX响应之前立即读取user

Try this: 尝试这个:

function userinfo() {    
    $.getJSON('###', function(data){
        var user = new Object();
        user.id = data.user.uID;
        user.name = data.user.uname;
        // Do your stuff here
        console.log(user);
    });
}

userinfo();

Answer to comment: Just have a function like this on the app: 发表评论的答案:在应用程序上只需具有以下功能:

function processUser(user){
   console.log(user);
}

Then use it in the AJAX callback, 然后在AJAX回调中使用它,

$.getJSON('###', function(data){
        var user = new Object();
        user.id = data.user.uID;
        user.name = data.user.uname;
        // Do your stuff here
        processUser(user);
    });

@Simon, you can do usual application logic in processUser() now, eg: @Simon,您现在可以在processUser()执行常规的应用程序逻辑,例如:

var usersList = []; // Assume this is a global for your app

function processUser(user){
   usersList.push(user);
   // now other parts of your app can find this user in the list
}
function userinfo(callback) {
    $.getJSON('###', function(data){
        callback({
            id: data.user.uId,
            name: data.user.uname
        });
    });
}
var user;
userinfo(function(newUser) {
    user = newUser;
    // do work with user.
});

// execution continues here before callback returns.  User will not be set.

user.name // error no property name of undefined.

Here's the fix: 解决方法是:

function userinfo() {
    var user = new Object();
    $.ajax({
        async: false, //solution right here
        url: '/login/user.html',
        success: function (data) {
            user.id = data.user.uID;
            user.name = data.user.uname;
        }
    });
    return user;
}
var user = userinfo();
console.log(user.id);

Works perfectly. 完美运作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM