使用Javascript Revealing Prototype Pattern从Ajax访问回调响应数据

时间:2016-09-22 14:24:41

标签: javascript prototype-pattern

我正在尝试使用Revealing Prototype Pattern构建JS代码。

我的基本用例是:创建不同类型的实体,如Person,Technology。每个实体都有自己的标签。为了得到这些标签,我做了一个ajax调用,它返回一个标签对象。我想在我的实现中访问此对象。但我不确定如何以正确的方式做到这一点。

我的尝试如下:

var Entity= function (url) {
    this.url = url; /*variable that can be shared by all instances*/
    var entityTags; 
};

Entity.prototype = function () {
    var create = function (type, values) {
    //code for creating
}

var update = function (type, values) {}

var tags = function () {
    AjaxCall(' ', this.url, {data:data}, 'callbackAfterGetTags', '');
    callbackAfterGetTags=function(responseFromAjax)
    {
        entityTags=responseFromAjax.tagsReturned; //how to access this entityTags in my implementation
    }
};
return {
     createEntity: create,
     getTagsEntity: tags
};

我的实施

var myEntity = new Entity(url);
myEntity.getTagsEntity();

Ajax调用成功返回对象,但我不确定如何以正确的方式访问标记函数内的对象。有什么建议?这是我在JS中首次使用OO样式的试用版。让我也知道我是否正确。

0 个答案:

没有答案