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, '' );

为什么?有什么意义,或者这只是一个虚拟循环?


在我看来,这里有两个主要问题。第一个是你没有包含DataManager构造函数。假设在您的代码中定义了构造函数:

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

DataManager 对象应在调用 init() 方法之前定义。我在您的代码中看到了另一个问题()。dataManager 变量未在 getData() 函数中声明/分配。在这种情况下使用对象级变量。试试这个:在 init 方法中 -

初始化:函数(){

      var instance = this;

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

并在 getData() 方法中使用

this.dataManager

代替

数据管理器

.

于 2012-10-08T10:12:13.717 回答