0

I have the following code.

function Test() {
this.funct_1 = function() {
    alert('funct_1');
}

this.funct_2 = function() {
    alert('funct_2');
}
return this;}

function getTestObj() {
var testObj;
if (!testObj) {
    testObj = new Test();
}
return function() {
    return testObj;
}}

What I'm trying to accomplish is the following. I want to have a class Test which is not singleton. Then in some other places in my application I need to have a function which could return the same instance per script execution. I figured that I could use closure for that getTestObj.

However, when I try to use it

getTestObj().funct_1();

I'm getting the following error, saying the funct_1() is not found.

Cannot find function funct_1 in object function () {...}.

Clearly, I'm making some kind of mistake here, but I'm not able to find any solution over the net which could help me. Would appreciate any comments.

NOTE: I'm forced to use ECMA5

vzhemevko
  • 815
  • 8
  • 25
  • @gurvinder372 In this case each time I call `getTestObj()` I would get a new instance of `Test` but I need only one instance per script execution. Also, I cannot expose any `var` outside the `getTestObj()` – vzhemevko Feb 16 '18 at 11:56
  • getTestObj is returning function so you have to call it as getTestObj()() this will return Test class's object. So answer is : getTestObj()().funct_1() – Sundar Singh Feb 16 '18 at 12:24

2 Answers2

1

testObj is wrapped inside a function

So, either call it

getTestObj()().funct_1(); //notice two ()()

Save the value of getTestObj() in a variable

var singleTon = getTestObj();
var testObj = singleTon();
testObj.funct_1();

Or, simply return testObj (in case singleTon isn't required)

function getTestObj() 
{
   var testObj;
   if (!testObj) {
      testObj = new Test();
   }
   return testObj;
}

And invoke it as

getTestObj().funct_1(); //notice single ()
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • with simply returning `return testObj` I will get a new instance each time I call `getTestObj()` however I need the same instance per script execution. With double `()()` I guess it's a solution however could it be avoided? I mean redesign it somehow to don't have the double `()()` – vzhemevko Feb 16 '18 at 11:59
  • @blisss05 I have updated the answer to make it clearer. – gurvinder372 Feb 16 '18 at 12:02
  • Thanks! One thing that I cannot expose any `var` outside the `getTestObj()` and it should be as only entry to get the `Test` instance. – vzhemevko Feb 16 '18 at 12:05
  • @blisss05 You don't have to, only `var singleTon` is declared and initialized outside. – gurvinder372 Feb 16 '18 at 12:07
  • Ok, I see. However, Is there any approach to don't use any additional `var` instances and use only closure here ? – vzhemevko Feb 16 '18 at 12:11
  • You need to save the reference to the `singleTon` somewhere or else you need to invoke `getTestObj()()` which will create a new Object. – gurvinder372 Feb 16 '18 at 12:13
0

getTestObj() is returning a function i.e. :

function() {
return testObj;
}

So you have to call it again getTestObj()(), this will return the Test's object and now you can access it's properties.

getTestObj()().funct_1();

OR

You can change your getTestObj function as :

function getTestObj() {
  var testObj;
  if (!testObj) {
     testObj = new Test();
  }
  return (function() {
        return testObj;
  }());
 }
Sundar Singh
  • 654
  • 5
  • 15