?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.

    评论

报告相同问题?