JS继承失败

时间:2015-04-25 19:34:25

标签: javascript node.js inheritance

我正在编写一个包含2个类(Object,ClientUser)的JS脚本,其中ClientUser来自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);
        }
    });
}

所以实际上每个东西都按预期工作,除了:在init方法中,在我执行post调用之后,我想迭代属性:this.properties,它应该包含在ClientUser中声明的值,但它是null。因为我可以在调用performPostCall之前调用this.className(返回正确的值),我想这是我使用的继承策略的一些问题。如果你们能帮助我,我真的很高兴,谢谢;)

0 个答案:

没有答案