0

I am writing a JS script with 2 classes(Object, ClientUser), where ClientUser inhertrce from Object:

function Object() {
    this.className;
    this.properties;
    this.editableproperties;

    this.set = function(key, value) {
        if (this.properties != undefined) {
            this.properties[key] = value;
        }

    }
    this.get = function(key) {
        if (this.properties != undefined) {
            return this.properties[key];
        }
    }
    this.init = function(objectID, completionHandler) {
        var getObjectURL = "/get_object_by_id";
        perfromPostCall(getObjectURL, {
            objectId: objectID,
            className: this.className
        }, function(result) {

            if (result.success == true) {
                for (var key in this.properties) {
                    alert("Find key:" + key);
                    this.set(key, result.object.get(key));
                }
                completionHandler(result);
            } else {
                completionHandler(result);
            }
        });
    }
}

function ClientUser() {
    ParseObject.prototype.constructor.apply(this);
    this.className = "User";
    this.properties = {
        createdAt: null,
        updatedAt: null,
        objectId: null,
        username: null,
        password: null,
        email: null,
        dateOfBirth: null,
        emailVarified: null,
        address: null,
        name: null,
        type: null
    };
}

function perfromPostCall(post_url, object, completionHandler) {
    $.ajax({
        url: post_url,
        type: "post",
        data: {
            object: object
        },
        success: function(result) {
            completionHandler(result);
        },
        error: function(error) {
            var result = {
                "succes": false,
                "msg": error
            };
            completionHandler(result);
        }
    });
}

So actually every thing works as expected except: In the init method, after I performed the post call I want to iterate over the properties: this.properties, which should contain the values declared in ClientUser, but it´s null. Since I can call this.className(which returns the right value), before I call performPostCall, I guess it´s some problem regarding the inheritance strategy I use. I would really be pleased if you guys could help me out, thanks ;)

Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
jona jürgen
  • 1,789
  • 4
  • 22
  • 31
  • 2
    shadowing Object ? that will replace all calls to Javascripts' Object and they all will fail. – webduvet Apr 25 '15 at 19:47
  • 2
    [How to access the correct `this` / context inside a callback?](http://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-context-inside-a-callback) -- Each `function` has its own `this` value, including those embedded within other functions, that's determined each time it's invoked. For `function(result)`, `this` likely won't still refer to the `ClientUser` instance. – Jonathan Lonowski Apr 25 '15 at 19:48
  • While I second webduvet's and Jonathan's points, please also notice that [it's not exactly best practise to use an async init function](http://stackoverflow.com/q/24398699/1048572) – Bergi Apr 25 '15 at 20:40

0 Answers0