?Briella 2017-01-25 11:45 采纳率: 0%
浏览 20

WeakMap和AJAX调用

While putting to practice what I've learned so far about ES2015 with Babel, specifically about WeakMaps, I came upon a problem I don't know why it's not working.

I have a WeakMap defined to house data coming from an AJAX call that's only triggered if the value of said WeakMap is undefined.

This is what I came up to:

class User {

    constructor( id ) {

        id = Number( id );

        if( id <= 0 || isNaN( id ) ) {
            throw new TypeError( 'Invalid User ID' );
        }

        _id.set( this, id );
    }

    getID() {
        return _id.get( this );
    }

    getData() {

        let _this = this;

        if( _data.get( _this ) === undefined ) {

            _this.loadData().done( function( data ) {

                // JSON is indeed successfully loaded

                console.log( data );

                _data.set( _this, data );

                // WeakMap is indeed set correctly

                console.log( _data.get( _this ) );
            });
        }

        // But here it's undefined again!

        console.log( _data.get( _this ) );

        return _data.get( _this );
    }

    loadData() {

        return $.get({
            url: '/users/' + _id.get( this, data ),
        });
    }
}

let _id   = new WeakMap;
let _data = new WeakMap;

// ---------------

var user = new User( 1 );

console.log( user.getID(), user.getData() ); // 1 undefined

As far as i know, I am setting the WeakMap data correctly, as the User ID is being set and can be retrieved, but the User Data coming from AJAX, although is indeed being set inside the jQuery.done() can't be accessed outside of it.

what i'm doing wrong?

  • 写回答

2条回答 默认 最新

  • weixin_33726943 2017-01-26 12:02
    关注

    I don't understand JavaScript to the point saying this is the right solution or not but I've searched A LOT, reading countless questions here in Stack Overflow with immense answers that fails to put things ins simple ways so, for anyone interested:

    class User {
    
        constructor( id ) {
    
            id = Number( id );
    
            if( id <= 0 || isNaN( id ) ) {
                throw new TypeError( 'Invalid User ID' );
            }
    
            _id.set( this, id );
        }
    
        getID() {
            return _id.get( this );
        }
    
        getData() {
    
            if( _data.get( this ) === undefined ) {
                _data.set( this, this.loadData() );
            }
    
            return _data.get( this );
        }
    
        loadData() {
            return $.getJSON( CARD_LIST + '/player/' + _id.get( this ) );
        }
    }
    
    let _data = new WeakMap;
    
    // ---------------
    
    var user = new User( 1 );
    
    user.getData().done( function( data ) {
    
        console.log( data );
    })
    

    It's not what I had in mind initially and I don't have knowledge to explain the "whys" but, at least this is a palpable working example that I humbly hope that will helps someone else who's trying to extract info from extremely long answers and/or unhelpful/unguided comments.

    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能