【发布时间】:2017-10-16 20:36:31
【问题描述】:
我是 react-native 编码的新手,但在 Objective-c 和 swift 编码方面有经验,并希望在 react-native 中使用单例模式。 我试图从其他 StackOverflow 答案中找出解决方案,但其中大多数只创建单例函数,如下代码:
var Singleton = (function () {
var instance;
function createInstance() {
var object = new Object("I am the instance");
return object;
}
return {
getInstance: function () {
if (!instance) {
instance = createInstance();
}
return instance;
}
};
})();
function run() {
var instance1 = Singleton.getInstance();
var instance2 = Singleton.getInstance();
alert("Same instance? " + (instance1 === instance2));
}
正如我们在上面的代码中看到的,我们创建的是单例函数而不是类。 如果有任何方法可以创建单例类并将该类中的多个变量作为objective-c 或swift 传递,请告诉我。 注意:如果我走错了方向,也请通知我。
【问题讨论】:
标签: reactjs react-native singleton