1

我的目标是将函数引用到单例/对象体中!

#

这个结果是我的目标:

var singletonObj_1 = (function () {
      return {
               custom_fn1 : function(){...},
               custom_fn2 : function(){...}    <<-- *But Imported
      }

})();

所以导入后,我可以调用 singletonObj_1.custom_fn2(); 但是 custom_fn2 来自另一个函数体,如何将它导入到上面的样子?

#

第二个函数“custom_fn2”未在上层函数 singletonObj_1 中声明!它应该被导入,所以结果如上所示!

singletonObj_1 的实际外观如何:

var singletonObj_1 = (function () {
      return {
               custom_fn1 : function(){...},
               *Here I need any kind of reference to singletonObj_2.custom_fn1 to look like above
      }

})();


var singletonObj_2 = function () {
      return {
               custom_fn1 : function(){...} *This function shall be exported into singletonObj_1
      }

}

#

我没有成功的尝试:

var singletonObj_1 = (function () {
      return {
               custom_fn1 : function(){...},

               (function(){
                    singletonObj_2(); // just to return custom_fn1 : function(){...} 
                })();
      }

})();

当然,我不能以这种方式在单例对象中声明自调用函数!希望我能表达我的问题!

提前感谢很多

真诚的 okyo

4

1 回答 1

0
var exported_fn = function () { ... };

var singletonObj_1 = (function () {
      return {
               custom_fn1 : function(){...},
               custom_fn2 : exported_fn
      };

})();


var singletonObj_2 = function () {
      return {
               custom_fn1 : exported_fn
      };
};
于 2013-01-04T00:55:52.577 回答