[英]How to access 'private functions' in a singleton from another object inside it
I am currently trying to create a test suite for my javascript apps. 我目前正在尝试为我的javascript应用程序创建一个测试套件。 My problem is that, it seems I cannot get access to init() from my utils object, as you can see below:
我的问题是,似乎我无法从我的utils对象访问init(),如下所示:
I have my app that follow a singleton pattern: 我的应用程序遵循单例模式:
var appModal = function () {
var utils = Object.create(moduleUtils);
function init(caller, options ) {
}
}();
My test suite is in moduleUtils, this is a object literal converted to a prototype 我的测试套件在moduleUtils中,这是一个转换为原型的对象文字
moduleUtils.debug = {
addSlideTest : function(){
/* this function cannot fire init() from appModal */
}}
This is not possible. 这是不可能的。
You need to expose the closured functions in a publicly visible object. 您需要在公开可见的对象中公开闭包函数。
For example, you can make a testMethods
object in your unit tests to collect private methods. 例如,您可以在单元测试中创建一个
testMethods
对象来收集私有方法。 Your main file would then add private methods to the object if it exists, like this: 然后,您的主文件将向对象添加私有方法(如果存在),如下所示:
//In appModal
if (typeof testMethods === "object")
testMethods.init = init;
//In test suite
testMethods = { };
...
testMethods.init();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.