我是react native编码方面的新手,但对objective-c和swift编码有经验,希望在react native中使用单例模式.
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));
}
正如我们在上面的代码中看到的,我们正在创建单例函数而不是类.