【问题标题】:How can you use ajax within an object to populate the object's instance variables?如何在对象中使用 ajax 来填充对象的实例变量?
【发布时间】:2009-10-03 00:37:16
【问题描述】:

我在层次结构中有多个对象,它们具有从超类继承的公共属性和方法,以及子类中特定于对象的属性和方法。我还是 OOP javascript 的新手,所以可能有更好的方法。我将 jQuery 用于 AJAX,但不确定这是否有任何区别。

function Obj(input) {
    this.in = input;
    this.out = {
        content: this.in,
        category: {},
        owner: utils.getValidUser(),
        state: 0,
        meta: {}
    };
    this.process = function() {
       console.log("No Process Defined");
    }
}

function MovieObj(input) {
    this.inheritFrom = Obj;
    this.inheritFrom();
    this.out.type = "movie";
}

function ActionMovie(input) {
        this.inheritFrom = MovieObj;
        this.inheritFrom();
        this.out.genre = "action";
        this.process = function() {
            console.log("movie search");
            $.getJSON("/api/search/"+ escape(this.out.content),
              function(data) {
                /* 
                   I want to modify the object properties according to
                   what comes back from the ajax call.
                */
            });
        }
}

【问题讨论】:

    标签: javascript ajax inheritance oop


    【解决方案1】:

    这是我早期代码的原型方法,以及对调用对象的简单引用,它解决了继承问题和范围问题。

    // Define Superclass
    function Obj(input) {
        this.content = input;
        this.type = "object";
        this.owner = utils.getValidUser();
        this.state = 0;
    }
    Obj.prototype.process = function() {
           console.log("No Process Defined");
    };
    
    
    // Define Movie Subclass
    function MovieObj(input) {
        Obj.call(this, input); 
        this.type = "movie";
    }
    MovieObj.prototype = new Obj();
    
    // Define ActionMovie as subclass of Movie and apply new process method.
    function ActionMovie(input) {
        MovieObj.call(this, input);
        this.genre = "action";
    }
    ActionMovie.prototype = new MovieObj();
    ActionMovie.prototype.process = function() {
                var _obj = this;
                $.getJSON("/api/search/"+ escape(this.content),
                  function(data) {
                    /* 
                       I want to modify the object properties according to
                       what comes back from the ajax call.
                    */
                    _obj.meta.title data.title;
                });
            }
    }
    

    现在这确实有效,但有一些警告。如前所述,每次定义新对象时都会调用超类的构造函数,因此会进行很多不必要的调用。

    此代码基于以下链接中包含的信息,该链接还描述了特定于 Mozilla 的解决方法: http://www.spheredev.org/wiki/Prototypes_in_JavaScript

    【讨论】:

      【解决方案2】:

      哦,面向对象 JavaScript 的方法有点奇怪 :)

      据我所知,您想要实现模型,您正在使用您熟悉的其他语言来构建您的对象。我大约是 this.inheritFrom 。在 JS 中没有类,对象直接从对象继承。一种方法(网络上有很多讨论)是使用 new 关键字,如

      var MovieObj = new Obj(input);
      MovieObj.out.type = "movie";
      

      你需要熟悉原型继承,你的代码看起来像

      function Obj(input){
          /* some stuff here */
      };
      
      function MovieObj(){};
      MovieObj.prototype = new Obj();
      MovieObj.prototype.constructor = MovieObj();
      MovieObj.protorype.out.type = 'movie';
      
      var actionMovie = new MovieObj();
      /* do some stuff here with your final object actionMovie */
      

      希望它能帮助您了解oo js 和其他oo 语言之间的区别。如果有任何不清楚的地方,请随时在 cmets 中提问(对不起我的英语)。

      【讨论】:

      • 谢谢。我以典型的方式重写了我的代码。事实证明这有点正确,但它并没有按书面规定工作。最主要的是您忘记在子类的构造函数中调用构造函数。 (见最终答案)。
      • 不错的代码,感谢您指出我错了。顺便说一句,你的编码语言背景是什么?
      猜你喜欢
      • 2021-05-26
      • 1970-01-01
      • 1970-01-01
      • 2017-04-16
      • 1970-01-01
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      • 2018-04-06
      相关资源
      最近更新 更多