0

作成しているオブジェクトのinit関数内で、別のオブジェクトの新しいインスタンスを作成したいと思います。

現時点では、私の初期化コードは次のようになっています。

    var myObj = {

getData: function(){

      var instance = this;

      // Display preloader gif
      instance.selectors.preloader.fadeIn();

      // Make call for appropriate data */
      dataManager.feed.get( function(data){

        for( var i = 0, len = data.feed.length; i < len; i++ ){

          var template = '';

        }
      }, window, null, '' );

      instance.displayVideos();

    },

    init: function(){

          var instance = this;

          // Create new DataManager object
          var dataManager = new DataManager();
    }



    }

myObj.init();

私の問題は、DataManagerが定義されていないというエラーが表示されることです。このオブジェクトを参照する方法を誰かが説明できますか?

4

2 に答える 2

2

ほら、あなたのコードは救い出せるかもしれませんが、それを維持するのは地獄のようになるでしょう。したがって、最終的に公開する前に、オブジェクトを必要なだけ準備できるクロージャを使用することをお勧めします。

var myObj = (function(current)
{
    'use strict';//<-- not necessary, but highly recommended
    var instance = {};//<-- this is going to become the value myObj references
    //if you want the init method, make a function object
    var init = function()
    {
        instance.dataManager = new DataManager();
    };
    instance.init = init;//<-- make it "public"
    //alternatively:
    instance.dataManager = new DataManager();
    //OR to have access to an instance of the DataManager object, but as a 
    // "private" property:
    var dataManager = new DataManager();
    //All methods or functions declared in this scope will have access to this object, 
    // the outer scope won't!
    //same for getData, use either one of the following:
    var getData = function()
    {
        //refer to the instance, not by using this, but use instance var
        //which is safer, and immutable thanks to the closure we're building
    };
    instance.getData = getData;
    //OR:
    instance.getData = function()
    {
        //same function body, just created and assigned directly
    };
    //if you chose to use the init method:
    instance.init();
    //if you created an init function, but didn't assign it to the instance object:
    init();
    //when the instance object is all set up and good to go:
    return instance;//will be assigned to the myObj variable
}(this));//call function and pass current scope as argument

次に、私が実際に取得できないコードは次のとおりです。

dataManager.feed.get( function(data)
{
    //...
    for( var i = 0, len = data.feed.length; i < len; i++ )
    {//loop through an array of sorts
         var template = '';//and initialize a variable to an empty string each time?
    }
}, window, null, '' );

なんで?ポイントは何ですか、それともこれは単なるダミーループですか?


私の見方では、ここには2つの大きな問題があります。DataManager1つ目は、コンストラクター関数を含めることができなかったことです。コンストラクターがコードで定義されていると仮定します。

var myObj = {
    init: function()
    {
        var instance = this;
        // Create new DataManager object
        var dataManager = new DataManager();
    },
    myObj.init();//<== this can't work
};

オブジェクトリテラルのメソッドを、まだ定義している間に呼び出しています。それは機能しません:

var myObj = {
    init: function()
    {
        var instance = this;
        // Create new DataManager object
        var dataManager = new DataManager();
    }
};
myObj.init();//<== now myObj exists, and has an init method
于 2012-10-08T10:14:26.357 に答える
0

init()メソッドを呼び出す前に、DataManagerオブジェクトを定義する必要があります。コード内にもう1つ問題があります()。dataManager変数は、getData()関数内で宣言/割り当てられていません。この場合、オブジェクトレベル変数を使用します。これを試してください:initメソッド内-

init:function(){

      var instance = this;

      // Create new DataManager object
      this.dataManager = new DataManager();  }

getData()メソッド内での使用

this.dataManager

それ以外の

データ管理者

于 2012-10-08T10:12:13.717 に答える