0

I have tried so many things, each and everyone either doesnt work, or breaks another part.

How can I get the following code to work, the requirements are:

  1. just one instance of the object
  2. i want to be able to pass in a parameter every time i use the object
  3. i dont want to pass it/use sub methods, such as getter or setter. not for this specific scenario.
  4. i want to be able to have private methods
  5. i want to be able to have public methods

Regarding #5, i have tried with this.func() and prototype.func() but cant get it to work.

I have tried but failed, but think perhaps i need to have a return value? or perhaps self-invoking functions?

below is example/pseudo code of what i have:

var myobj = new function(param){

  //code here, able to use input param and private function 'privatefunc' below

  var privatefunc=function(){
    console.log("i am a private function");
  }
  this.publicfunc=function(){
     console.log("i am a public function");
  }
}



myobj("batman");
myobj("superman");
myobj("catwoman");
myobj().publfunc(); // should print out: i am a public function

thank you for your time!
Jason
  • 387
  • 3
  • 13
  • 1
    Feels like [an XY problem](https://meta.stackexchange.com/questions/346503/what-is-the-opposite-of-the-xy-problem). You have a list of requirements but they aren't really solving a specific problem. What are you actually trying to do that needs these requirements? Perhaps there is an easier way to go about it. – VLAZ Sep 27 '20 at 19:41
  • You are calling a function, not instantiating a *class*. You need to learn a bit about JavaScript classes, prototypes and such. Is a bit long to explain here. Just search in Google "javascript es5 class instantiation" (as per your code you are looking for *old* classes instead of new ES6 ones). – Jorge Fuentes González Sep 27 '20 at 19:43
  • 1
    On a separate note, the second point is very unclear. What do you mean "pass a parameter when you use the object"? You either create one instance or many, the constructor should take your parameters. If you need to re-initialise an instance, then you might need multiple ones or perhaps not an object at all. – VLAZ Sep 27 '20 at 19:43
  • Does this answer your question? [Simplest/Cleanest way to implement singleton in JavaScript?](https://stackoverflow.com/questions/1479319/simplest-cleanest-way-to-implement-singleton-in-javascript) – Easwar Sep 27 '20 at 19:44
  • @JorgeFuentesGonzález calling a function with `new` will construct a new object and return it as the result. [What is the 'new' keyword in JavaScript?](https://stackoverflow.com/q/1646698) – VLAZ Sep 27 '20 at 19:44
  • @VLAZ Yeah, I know, and it seems that is what he wants with the `this.publicfunc` thingy, but he needs to know what is going on when using the `new` keyword and how prototypes work. – Jorge Fuentes González Sep 27 '20 at 19:45
  • @JorgeFuentesGonzález I think we all need to know what the actual goal is here because otherwise none of us can get anywhere with solving it. – VLAZ Sep 27 '20 at 19:48
  • @VLAZ Yeah, that's true. For me seems homework or alike actually. – Jorge Fuentes González Sep 27 '20 at 19:50
  • 1
    [Never use `new function(){…}`.](https://stackoverflow.com/questions/10406552/is-it-right-to-think-of-a-javascript-function-expression-that-uses-the-new-key-as-static). Your `param` won't work. – Bergi Sep 27 '20 at 19:51
  • no its not homework. ok, how about ignoring everything exept getting public method to work? please. – Jason Sep 27 '20 at 19:54
  • @Easwar there is some interesting answers there for sure, specifically answer by user 'skalee' . just the public/private needed as well. – Jason Sep 27 '20 at 20:17

0 Answers0